silverstripe /
silverstripe-subsites
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\Subsites\Tests; |
||
| 4 | |||
| 5 | use Page; |
||
|
0 ignored issues
–
show
|
|||
| 6 | use SilverStripe\Assets\File; |
||
| 7 | use SilverStripe\Assets\Filesystem; |
||
| 8 | use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore; |
||
|
0 ignored issues
–
show
The type
SilverStripe\Assets\Test...toreTest\TestAssetStore was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 9 | use SilverStripe\CMS\Model\SiteTree; |
||
| 10 | use SilverStripe\Control\Director; |
||
| 11 | use SilverStripe\Core\Config\Config; |
||
| 12 | use SilverStripe\ORM\DB; |
||
| 13 | use SilverStripe\Subsites\Model\Subsite; |
||
| 14 | use SilverStripe\Subsites\Pages\SubsitesVirtualPage; |
||
| 15 | use SilverStripe\Versioned\Versioned; |
||
| 16 | |||
| 17 | class SubsitesVirtualPageTest extends BaseSubsiteTest |
||
| 18 | { |
||
| 19 | protected static $fixture_file = [ |
||
| 20 | 'SubsiteTest.yml', |
||
| 21 | 'SubsitesVirtualPageTest.yml', |
||
| 22 | ]; |
||
| 23 | |||
| 24 | protected function setUp() |
||
| 25 | { |
||
| 26 | parent::setUp(); |
||
| 27 | |||
| 28 | Config::modify()->set(Subsite::class, 'write_hostmap', false); |
||
| 29 | |||
| 30 | // Set backend root to /DataDifferencerTest |
||
| 31 | TestAssetStore::activate('SubsitesVirtualPageTest'); |
||
| 32 | |||
| 33 | // Create a test files for each of the fixture references |
||
| 34 | $file = $this->objFromFixture(File::class, 'file1'); |
||
| 35 | $page = $this->objFromFixture(SiteTree::class, 'page1'); |
||
| 36 | $fromPath = __DIR__ . '/testscript-test-file.pdf'; |
||
| 37 | $destPath = TestAssetStore::getLocalPath($file); |
||
| 38 | Filesystem::makeFolder(dirname($destPath)); |
||
| 39 | copy($fromPath, $destPath); |
||
| 40 | |||
| 41 | // Hack in site link tracking after the fact |
||
| 42 | $page->Content = '<p><img src="' . $file->getURL() . '" data-fileid="' . $file->ID . '" /></p>'; |
||
| 43 | $page->write(); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function tearDown() |
||
| 47 | { |
||
| 48 | TestAssetStore::reset(); |
||
| 49 | parent::tearDown(); |
||
| 50 | } |
||
| 51 | |||
| 52 | // Attempt to bring main:linky to subsite2:linky |
||
| 53 | public function testVirtualPageFromAnotherSubsite() |
||
| 54 | { |
||
| 55 | $subsite = $this->objFromFixture(Subsite::class, 'subsite2'); |
||
| 56 | |||
| 57 | Subsite::changeSubsite($subsite->ID); |
||
| 58 | Subsite::$disable_subsite_filter = false; |
||
| 59 | |||
| 60 | $linky = $this->objFromFixture(Page::class, 'linky'); |
||
| 61 | |||
| 62 | $svp = new SubsitesVirtualPage(); |
||
| 63 | $svp->CopyContentFromID = $linky->ID; |
||
| 64 | $svp->SubsiteID = $subsite->ID; |
||
| 65 | $svp->URLSegment = 'linky'; |
||
| 66 | |||
| 67 | $svp->write(); |
||
| 68 | |||
| 69 | $this->assertEquals($svp->SubsiteID, $subsite->ID); |
||
|
0 ignored issues
–
show
The property
SubsiteID does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 70 | $this->assertEquals($svp->Title, $linky->Title); |
||
|
0 ignored issues
–
show
The property
Title does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 71 | } |
||
| 72 | |||
| 73 | public function testFileLinkRewritingOnVirtualPages() |
||
| 74 | { |
||
| 75 | $this->markTestSkipped('File handling needs update'); |
||
| 76 | // File setup |
||
| 77 | $this->logInWithPermission('ADMIN'); |
||
| 78 | touch(Director::baseFolder() . '/assets/testscript-test-file.pdf'); |
||
| 79 | |||
| 80 | // Publish the source page |
||
| 81 | $page = $this->objFromFixture(SiteTree::class, 'page1'); |
||
| 82 | $this->assertTrue($page->publishSingle()); |
||
| 83 | |||
| 84 | // Create a virtual page from it, and publish that |
||
| 85 | $svp = new SubsitesVirtualPage(); |
||
| 86 | $svp->CopyContentFromID = $page->ID; |
||
| 87 | $svp->write(); |
||
| 88 | $svp->publishSingle(); |
||
| 89 | |||
| 90 | // Rename the file |
||
| 91 | $file = $this->objFromFixture(File::class, 'file1'); |
||
| 92 | $file->Name = 'renamed-test-file.pdf'; |
||
| 93 | $file->write(); |
||
| 94 | |||
| 95 | // Verify that the draft and publish virtual pages both have the corrected link |
||
| 96 | $this->assertContains( |
||
| 97 | '<img src="/assets/SubsitesVirtualPageTest/464dedb70a/renamed-test-file.pdf"', |
||
| 98 | DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $svp->ID")->value() |
||
| 99 | ); |
||
| 100 | $this->assertContains( |
||
| 101 | '<img src="/assets/SubsitesVirtualPageTest/464dedb70a/renamed-test-file.pdf"', |
||
| 102 | DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $svp->ID")->value() |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function testSubsiteVirtualPagesArentInappropriatelyPublished() |
||
| 107 | { |
||
| 108 | $this->markTestSkipped('Needs some update or refactoring'); |
||
| 109 | // Fixture |
||
| 110 | $p = new Page(); |
||
| 111 | $p->Content = 'test content'; |
||
| 112 | $p->write(); |
||
| 113 | $vp = new SubsitesVirtualPage(); |
||
| 114 | $vp->CopyContentFromID = $p->ID; |
||
| 115 | $vp->write(); |
||
| 116 | |||
| 117 | // VP is oragne |
||
| 118 | $this->assertTrue($vp->IsAddedToStage); |
||
| 119 | |||
| 120 | // VP is still orange after we publish |
||
| 121 | $p->publishSingle(); |
||
| 122 | $this->fixVersionNumberCache($vp); |
||
| 123 | $this->assertTrue($vp->IsAddedToStage); |
||
| 124 | |||
| 125 | // A new VP created after P's initial construction |
||
| 126 | $vp2 = new SubsitesVirtualPage(); |
||
| 127 | $vp2->CopyContentFromID = $p->ID; |
||
| 128 | $vp2->write(); |
||
| 129 | $this->assertTrue($vp2->IsAddedToStage); |
||
| 130 | |||
| 131 | // Also remains orange after a republish |
||
| 132 | $p->Content = 'new content'; |
||
| 133 | $p->write(); |
||
| 134 | $p->publishSingle(); |
||
| 135 | $this->fixVersionNumberCache($vp2); |
||
| 136 | $this->assertTrue($vp2->IsAddedToStage); |
||
| 137 | |||
| 138 | // VP is now published |
||
| 139 | $vp->publishSingle(); |
||
| 140 | |||
| 141 | $this->fixVersionNumberCache($vp); |
||
| 142 | $this->assertTrue($vp->ExistsOnLive); |
||
| 143 | $this->assertFalse($vp->IsModifiedOnStage); |
||
| 144 | |||
| 145 | // P edited, VP and P both go green |
||
| 146 | $p->Content = 'third content'; |
||
| 147 | $p->write(); |
||
| 148 | |||
| 149 | $this->fixVersionNumberCache($vp, $p); |
||
| 150 | $this->assertTrue($p->IsModifiedOnStage); |
||
| 151 | $this->assertTrue($vp->IsModifiedOnStage); |
||
| 152 | |||
| 153 | // Publish, VP goes black |
||
| 154 | $p->publishSingle(); |
||
| 155 | $this->fixVersionNumberCache($vp); |
||
| 156 | $this->assertTrue($vp->ExistsOnLive); |
||
| 157 | $this->assertFalse($vp->IsModifiedOnStage); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * This test ensures published Subsites Virtual Pages immediately reflect updates |
||
| 162 | * to their published target pages. Note - this has to happen when the virtual page |
||
| 163 | * is in a different subsite to the page you are editing and republishing, |
||
| 164 | * otherwise the test will pass falsely due to current subsite ID being the same. |
||
| 165 | */ |
||
| 166 | public function testPublishedSubsiteVirtualPagesUpdateIfTargetPageUpdates() |
||
| 167 | { |
||
| 168 | $this->markTestSkipped('Needs some update or refactoring'); |
||
| 169 | |||
| 170 | // create page |
||
| 171 | $p = new Page(); |
||
| 172 | $p->Content = 'Content'; |
||
| 173 | $p->Title = 'Title'; |
||
| 174 | $p->writeToStage('Stage'); |
||
| 175 | $p->copyVersionToStage('Stage', 'Live'); |
||
| 176 | $this->assertTrue($p->ExistsOnLive); |
||
| 177 | |||
| 178 | // change to subsite |
||
| 179 | $subsite = $this->objFromFixture(Subsite::class, 'subsite2'); |
||
| 180 | Subsite::changeSubsite($subsite->ID); |
||
| 181 | Subsite::$disable_subsite_filter = false; |
||
| 182 | |||
| 183 | // create svp in subsite |
||
| 184 | $svp = new SubsitesVirtualPage(); |
||
| 185 | $svp->CopyContentFromID = $p->ID; |
||
| 186 | $svp->write(); |
||
| 187 | $svp->writeToStage('Stage'); |
||
| 188 | $svp->copyVersionToStage('Stage', 'Live'); |
||
| 189 | $this->assertEquals($svp->SubsiteID, $subsite->ID); |
||
| 190 | $this->assertTrue($svp->ExistsOnLive); |
||
| 191 | |||
| 192 | // change back to original subsite ("Main site") |
||
| 193 | Subsite::changeSubsite(0); |
||
| 194 | |||
| 195 | // update original page |
||
| 196 | $p->Title = 'New Title'; |
||
| 197 | // "save & publish" |
||
| 198 | $p->writeToStage('Stage'); |
||
| 199 | $p->copyVersionToStage('Stage', 'Live'); |
||
| 200 | $this->assertNotEquals($p->SubsiteID, $subsite->ID); |
||
| 201 | |||
| 202 | // reload SVP from database |
||
| 203 | // can't use DO::get by id because caches. |
||
| 204 | $svpdb = $svp->get()->byID($svp->ID); |
||
| 205 | |||
| 206 | // ensure title changed |
||
| 207 | $this->assertEquals($svpdb->Title, $p->Title); |
||
| 208 | } |
||
| 209 | |||
| 210 | public function testUnpublishingParentPageUnpublishesSubsiteVirtualPages() |
||
| 211 | { |
||
| 212 | $this->markTestIncomplete('@todo fix this test'); |
||
| 213 | |||
| 214 | Config::modify()->set('StaticPublisher', 'disable_realtime', true); |
||
| 215 | |||
| 216 | // Go to main site, get parent page |
||
| 217 | $subsite = $this->objFromFixture(Subsite::class, 'main'); |
||
| 218 | Subsite::changeSubsite($subsite->ID); |
||
| 219 | $page = $this->objFromFixture('Page', 'importantpage'); |
||
| 220 | |||
| 221 | // Create two SVPs on other subsites |
||
| 222 | $subsite = $this->objFromFixture(Subsite::class, 'subsite1'); |
||
| 223 | Subsite::changeSubsite($subsite->ID); |
||
| 224 | $vp1 = new SubsitesVirtualPage(); |
||
| 225 | $vp1->CopyContentFromID = $page->ID; |
||
| 226 | $vp1->write(); |
||
| 227 | $vp1->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
||
| 228 | |||
| 229 | $subsite = $this->objFromFixture(Subsite::class, 'subsite2'); |
||
| 230 | Subsite::changeSubsite($subsite->ID); |
||
| 231 | $vp2 = new SubsitesVirtualPage(); |
||
| 232 | $vp2->CopyContentFromID = $page->ID; |
||
| 233 | $vp2->write(); |
||
| 234 | $vp2->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
||
| 235 | |||
| 236 | // Switch back to main site, unpublish source |
||
| 237 | $subsite = $this->objFromFixture(Subsite::class, 'main'); |
||
| 238 | Subsite::changeSubsite($subsite->ID); |
||
| 239 | $page = $this->objFromFixture('Page', 'importantpage'); |
||
| 240 | $page->doUnpublish(); |
||
| 241 | |||
| 242 | Subsite::changeSubsite($vp1->SubsiteID); |
||
| 243 | $onLive = Versioned::get_one_by_stage(SubsitesVirtualPage::class, 'Live', '"SiteTree_Live"."ID" = ' . $vp1->ID); |
||
| 244 | $this->assertNull($onLive, 'SVP has been removed from live'); |
||
| 245 | |||
| 246 | $subsite = $this->objFromFixture(Subsite::class, 'subsite2'); |
||
|
0 ignored issues
–
show
|
|||
| 247 | Subsite::changeSubsite($vp2->SubsiteID); |
||
| 248 | $onLive = Versioned::get_one_by_stage(SubsitesVirtualPage::class, 'Live', '"SiteTree_Live"."ID" = ' . $vp2->ID); |
||
| 249 | $this->assertNull($onLive, 'SVP has been removed from live'); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Similar to {@link SiteTreeSubsitesTest->testTwoPagesWithSameURLOnDifferentSubsites()} |
||
| 254 | * and {@link SiteTreeSubsitesTest->testPagesInDifferentSubsitesCanShareURLSegment()}. |
||
| 255 | */ |
||
| 256 | public function testSubsiteVirtualPageCanHaveSameUrlsegmentAsOtherSubsite() |
||
| 257 | { |
||
| 258 | $this->markTestIncomplete('@todo fix this test'); |
||
| 259 | |||
| 260 | $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); |
||
| 261 | $subsite2 = $this->objFromFixture(Subsite::class, 'subsite2'); |
||
| 262 | Subsite::changeSubsite($subsite1->ID); |
||
| 263 | |||
| 264 | $subsite1Page = $this->objFromFixture(Page::class, 'subsite1_staff'); |
||
| 265 | $subsite1Page->URLSegment = 'staff'; |
||
| 266 | $subsite1Page->write(); |
||
| 267 | |||
| 268 | // saving on subsite1, and linking to subsite1 |
||
| 269 | $subsite1Vp = new SubsitesVirtualPage(); |
||
| 270 | $subsite1Vp->CopyContentFromID = $subsite1Page->ID; |
||
| 271 | $subsite1Vp->SubsiteID = $subsite1->ID; |
||
| 272 | $subsite1Vp->write(); |
||
| 273 | |||
| 274 | $this->assertNotEquals( |
||
| 275 | $subsite1Vp->URLSegment, |
||
| 276 | $subsite1Page->URLSegment, |
||
| 277 | "Doesn't allow explicit URLSegment overrides when already existing in same subsite" |
||
| 278 | ); |
||
| 279 | |||
| 280 | //Change to subsite 2 |
||
| 281 | Subsite::changeSubsite($subsite2->ID); |
||
| 282 | |||
| 283 | // saving in subsite2 (which already has a page with URLSegment 'contact-us'), |
||
| 284 | // but linking to a page in subsite1 |
||
| 285 | $subsite2Vp = new SubsitesVirtualPage(); |
||
| 286 | $subsite2Vp->CopyContentFromID = $subsite1Page->ID; |
||
| 287 | $subsite2Vp->SubsiteID = $subsite2->ID; |
||
| 288 | $subsite2Vp->write(); |
||
| 289 | $this->assertEquals( |
||
| 290 | $subsite2Vp->URLSegment, |
||
| 291 | $subsite1Page->URLSegment, |
||
| 292 | 'Does allow explicit URLSegment overrides when only existing in a different subsite' |
||
| 293 | ); |
||
| 294 | |||
| 295 | // When changing subsites and re-saving this page, it doesn't trigger a change |
||
| 296 | Subsite::changeSubsite($subsite1->ID); |
||
| 297 | $subsite1Page->write(); |
||
| 298 | $subsite2Vp->write(); |
||
| 299 | $this->assertEquals( |
||
| 300 | $subsite2Vp->URLSegment, |
||
| 301 | $subsite1Page->URLSegment, |
||
| 302 | "SubsiteVirtualPage doesn't change urls when being written in another subsite" |
||
| 303 | ); |
||
| 304 | } |
||
| 305 | |||
| 306 | protected function fixVersionNumberCache($page) |
||
| 307 | { |
||
| 308 | $pages = func_get_args(); |
||
| 309 | foreach ($pages as $p) { |
||
| 310 | Versioned::prepopulate_versionnumber_cache(SiteTree::class, 'Stage', [$p->ID]); |
||
| 311 | Versioned::prepopulate_versionnumber_cache(SiteTree::class, 'Live', [$p->ID]); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | public function testValidURLSegmentWithUniquePageAndNestedURLs() |
||
| 316 | { |
||
| 317 | SiteTree::config()->set('nested_urls', true); |
||
| 318 | |||
| 319 | $newPage = new SubsitesVirtualPage(); |
||
| 320 | $newPage->Title = 'My new page'; |
||
| 321 | $newPage->URLSegment = 'my-new-page'; |
||
| 322 | |||
| 323 | $this->assertTrue($newPage->validURLSegment()); |
||
| 324 | } |
||
| 325 | |||
| 326 | public function testValidURLSegmentWithExistingPageInSubsite() |
||
| 327 | { |
||
| 328 | $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); |
||
| 329 | Subsite::changeSubsite($subsite1->ID); |
||
| 330 | |||
| 331 | SiteTree::config()->set('nested_urls', false); |
||
| 332 | |||
| 333 | $similarContactUsPage = new SubsitesVirtualPage(); |
||
| 334 | $similarContactUsPage->Title = 'Similar to Contact Us in Subsite 1'; |
||
| 335 | $similarContactUsPage->URLSegment = 'contact-us'; |
||
| 336 | |||
| 337 | $this->assertFalse($similarContactUsPage->validURLSegment()); |
||
| 338 | } |
||
| 339 | |||
| 340 | public function testValidURLSegmentWithExistingPageInAnotherSubsite() |
||
| 341 | { |
||
| 342 | $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); |
||
| 343 | Subsite::changeSubsite($subsite1->ID); |
||
| 344 | |||
| 345 | $similarStaffPage = new SubsitesVirtualPage(); |
||
| 346 | $similarStaffPage->Title = 'Similar to Staff page in main site'; |
||
| 347 | $similarStaffPage->URLSegment = 'staff'; |
||
| 348 | |||
| 349 | $this->assertFalse($similarStaffPage->validURLSegment()); |
||
| 350 | } |
||
| 351 | } |
||
| 352 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths