Passed
Pull Request — master (#326)
by Robbie
04:32
created

SubsitesVirtualPageTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 300
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 300
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 4 1
A setUp() 0 18 1
B testPublishedSubsiteVirtualPagesUpdateIfTargetPageUpdates() 0 42 1
A testVirtualPageFromAnotherSubsite() 0 20 1
B testFileLinkRewritingOnVirtualPages() 0 30 1
A testSubsiteVirtualPageCanHaveSameUrlsegmentAsOtherSubsite() 0 48 1
A fixVersionNumberCache() 0 6 2
A testSubsiteVirtualPagesArentInappropriatelyPublished() 0 52 1
B testUnpublishingParentPageUnpublishesSubsiteVirtualPages() 0 40 1
1
<?php
2
3
namespace SilverStripe\Subsites\Tests;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Filesystem;
8
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
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 static $illegal_extensions = [
25
        SiteTree::class => ['Translatable'] // @todo implement Translatable namespace
26
    ];
27
28
    protected function setUp()
29
    {
30
        parent::setUp();
31
32
        // Set backend root to /DataDifferencerTest
33
        TestAssetStore::activate('SubsitesVirtualPageTest');
34
35
        // Create a test files for each of the fixture references
36
        $file = $this->objFromFixture(File::class, 'file1');
37
        $page = $this->objFromFixture(SiteTree::class, 'page1');
38
        $fromPath = __DIR__ . '/testscript-test-file.pdf';
39
        $destPath = TestAssetStore::getLocalPath($file);
0 ignored issues
show
Bug introduced by
$file of type SilverStripe\ORM\DataObject is incompatible with the type SilverStripe\Assets\Storage\AssetContainer expected by parameter $asset of SilverStripe\Assets\Test...etStore::getLocalPath(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $destPath = TestAssetStore::getLocalPath(/** @scrutinizer ignore-type */ $file);
Loading history...
40
        Filesystem::makeFolder(dirname($destPath));
41
        copy($fromPath, $destPath);
42
43
        // Hack in site link tracking after the fact
44
        $page->Content = '<p><img src="' . $file->getURL() . '" data-fileid="' . $file->ID . '" /></p>';
45
        $page->write();
46
    }
47
48
    public function tearDown()
49
    {
50
        TestAssetStore::reset();
51
        parent::tearDown();
52
    }
53
54
    // Attempt to bring main:linky to subsite2:linky
55
    public function testVirtualPageFromAnotherSubsite()
56
    {
57
        Subsite::$write_hostmap = false;
58
59
        $subsite = $this->objFromFixture(Subsite::class, 'subsite2');
60
61
        Subsite::changeSubsite($subsite->ID);
62
        Subsite::$disable_subsite_filter = false;
63
64
        $linky = $this->objFromFixture(Page::class, 'linky');
65
66
        $svp = new SubsitesVirtualPage();
67
        $svp->CopyContentFromID = $linky->ID;
68
        $svp->SubsiteID = $subsite->ID;
69
        $svp->URLSegment = 'linky';
70
71
        $svp->write();
72
73
        $this->assertEquals($svp->SubsiteID, $subsite->ID);
0 ignored issues
show
Bug Best Practice introduced by
The property SubsiteID does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
74
        $this->assertEquals($svp->Title, $linky->Title);
0 ignored issues
show
Bug Best Practice introduced by
The property Title does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
75
    }
76
77
    public function testFileLinkRewritingOnVirtualPages()
78
    {
79
        $this->markTestSkipped('File handling needs update');
80
        // File setup
81
        $this->logInWithPermission('ADMIN');
82
        touch(Director::baseFolder() . '/assets/testscript-test-file.pdf');
83
84
        // Publish the source page
85
        $page = $this->objFromFixture(SiteTree::class, 'page1');
86
        $this->assertTrue($page->publishSingle());
87
88
        // Create a virtual page from it, and publish that
89
        $svp = new SubsitesVirtualPage();
90
        $svp->CopyContentFromID = $page->ID;
91
        $svp->write();
92
        $svp->publishSingle();
93
94
        // Rename the file
95
        $file = $this->objFromFixture(File::class, 'file1');
96
        $file->Name = 'renamed-test-file.pdf';
97
        $file->write();
98
99
        // Verify that the draft and publish virtual pages both have the corrected link
100
        $this->assertContains(
101
            '<img src="/assets/SubsitesVirtualPageTest/464dedb70a/renamed-test-file.pdf"',
102
            DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $svp->ID")->value()
103
        );
104
        $this->assertContains(
105
            '<img src="/assets/SubsitesVirtualPageTest/464dedb70a/renamed-test-file.pdf"',
106
            DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $svp->ID")->value()
107
        );
108
    }
109
110
    public function testSubsiteVirtualPagesArentInappropriatelyPublished()
111
    {
112
        $this->markTestSkipped('Needs some update or refactoring');
113
        // Fixture
114
        $p = new Page();
115
        $p->Content = 'test content';
116
        $p->write();
117
        $vp = new SubsitesVirtualPage();
118
        $vp->CopyContentFromID = $p->ID;
119
        $vp->write();
120
121
        // VP is oragne
122
        $this->assertTrue($vp->IsAddedToStage);
123
124
        // VP is still orange after we publish
125
        $p->publishSingle();
126
        $this->fixVersionNumberCache($vp);
127
        $this->assertTrue($vp->IsAddedToStage);
128
129
        // A new VP created after P's initial construction
130
        $vp2 = new SubsitesVirtualPage();
131
        $vp2->CopyContentFromID = $p->ID;
132
        $vp2->write();
133
        $this->assertTrue($vp2->IsAddedToStage);
134
135
        // Also remains orange after a republish
136
        $p->Content = 'new content';
137
        $p->write();
138
        $p->publishSingle();
139
        $this->fixVersionNumberCache($vp2);
140
        $this->assertTrue($vp2->IsAddedToStage);
141
142
        // VP is now published
143
        $vp->publishSingle();
144
145
        $this->fixVersionNumberCache($vp);
146
        $this->assertTrue($vp->ExistsOnLive);
147
        $this->assertFalse($vp->IsModifiedOnStage);
148
149
        // P edited, VP and P both go green
150
        $p->Content = 'third content';
151
        $p->write();
152
153
        $this->fixVersionNumberCache($vp, $p);
154
        $this->assertTrue($p->IsModifiedOnStage);
155
        $this->assertTrue($vp->IsModifiedOnStage);
156
157
        // Publish, VP goes black
158
        $p->publishSingle();
159
        $this->fixVersionNumberCache($vp);
160
        $this->assertTrue($vp->ExistsOnLive);
161
        $this->assertFalse($vp->IsModifiedOnStage);
162
    }
163
164
    /**
165
     * This test ensures published Subsites Virtual Pages immediately reflect updates
166
     * to their published target pages. Note - this has to happen when the virtual page
167
     * is in a different subsite to the page you are editing and republishing,
168
     * otherwise the test will pass falsely due to current subsite ID being the same.
169
     */
170
    public function testPublishedSubsiteVirtualPagesUpdateIfTargetPageUpdates()
171
    {
172
        $this->markTestSkipped('Needs some update or refactoring');
173
174
        // create page
175
        $p = new Page();
176
        $p->Content = 'Content';
177
        $p->Title = 'Title';
178
        $p->writeToStage('Stage');
179
        $p->copyVersionToStage('Stage', 'Live');
180
        $this->assertTrue($p->ExistsOnLive);
181
182
        // change to subsite
183
        $subsite = $this->objFromFixture(Subsite::class, 'subsite2');
184
        Subsite::changeSubsite($subsite->ID);
185
        Subsite::$disable_subsite_filter = false;
186
187
        // create svp in subsite
188
        $svp = new SubsitesVirtualPage();
189
        $svp->CopyContentFromID = $p->ID;
190
        $svp->write();
191
        $svp->writeToStage('Stage');
192
        $svp->copyVersionToStage('Stage', 'Live');
193
        $this->assertEquals($svp->SubsiteID, $subsite->ID);
194
        $this->assertTrue($svp->ExistsOnLive);
195
196
        // change back to original subsite ("Main site")
197
        Subsite::changeSubsite(0);
198
199
        // update original page
200
        $p->Title = 'New Title';
201
        // "save & publish"
202
        $p->writeToStage('Stage');
203
        $p->copyVersionToStage('Stage', 'Live');
204
        $this->assertNotEquals($p->SubsiteID, $subsite->ID);
205
206
        // reload SVP from database
207
        // can't use DO::get by id because caches.
208
        $svpdb = $svp->get()->byID($svp->ID);
209
210
        // ensure title changed
211
        $this->assertEquals($svpdb->Title, $p->Title);
212
    }
213
214
    public function testUnpublishingParentPageUnpublishesSubsiteVirtualPages()
215
    {
216
        $this->markTestIncomplete('@todo fix this test');
217
218
        Config::modify()->set('StaticPublisher', 'disable_realtime', true);
219
220
        // Go to main site, get parent page
221
        $subsite = $this->objFromFixture(Subsite::class, 'main');
222
        Subsite::changeSubsite($subsite->ID);
223
        $page = $this->objFromFixture('Page', 'importantpage');
224
225
        // Create two SVPs on other subsites
226
        $subsite = $this->objFromFixture(Subsite::class, 'subsite1');
227
        Subsite::changeSubsite($subsite->ID);
228
        $vp1 = new SubsitesVirtualPage();
229
        $vp1->CopyContentFromID = $page->ID;
230
        $vp1->write();
231
        $vp1->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
232
233
        $subsite = $this->objFromFixture(Subsite::class, 'subsite2');
234
        Subsite::changeSubsite($subsite->ID);
235
        $vp2 = new SubsitesVirtualPage();
236
        $vp2->CopyContentFromID = $page->ID;
237
        $vp2->write();
238
        $vp2->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
239
240
        // Switch back to main site, unpublish source
241
        $subsite = $this->objFromFixture(Subsite::class, 'main');
242
        Subsite::changeSubsite($subsite->ID);
243
        $page = $this->objFromFixture('Page', 'importantpage');
244
        $page->doUnpublish();
245
246
        Subsite::changeSubsite($vp1->SubsiteID);
247
        $onLive = Versioned::get_one_by_stage(SubsitesVirtualPage::class, 'Live', '"SiteTree_Live"."ID" = ' . $vp1->ID);
248
        $this->assertNull($onLive, 'SVP has been removed from live');
249
250
        $subsite = $this->objFromFixture(Subsite::class, 'subsite2');
0 ignored issues
show
Unused Code introduced by
The assignment to $subsite is dead and can be removed.
Loading history...
251
        Subsite::changeSubsite($vp2->SubsiteID);
252
        $onLive = Versioned::get_one_by_stage(SubsitesVirtualPage::class, 'Live', '"SiteTree_Live"."ID" = ' . $vp2->ID);
253
        $this->assertNull($onLive, 'SVP has been removed from live');
254
    }
255
256
    /**
257
     * Similar to {@link SiteTreeSubsitesTest->testTwoPagesWithSameURLOnDifferentSubsites()}
258
     * and {@link SiteTreeSubsitesTest->testPagesInDifferentSubsitesCanShareURLSegment()}.
259
     */
260
    public function testSubsiteVirtualPageCanHaveSameUrlsegmentAsOtherSubsite()
261
    {
262
        $this->markTestIncomplete('@todo fix this test');
263
264
        Subsite::$write_hostmap = false;
265
        $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
266
        $subsite2 = $this->objFromFixture(Subsite::class, 'subsite2');
267
        Subsite::changeSubsite($subsite1->ID);
268
269
        $subsite1Page = $this->objFromFixture(Page::class, 'subsite1_staff');
270
        $subsite1Page->URLSegment = 'staff';
271
        $subsite1Page->write();
272
273
        // saving on subsite1, and linking to subsite1
274
        $subsite1Vp = new SubsitesVirtualPage();
275
        $subsite1Vp->CopyContentFromID = $subsite1Page->ID;
276
        $subsite1Vp->SubsiteID = $subsite1->ID;
277
        $subsite1Vp->write();
278
279
        $this->assertNotEquals(
280
            $subsite1Vp->URLSegment,
281
            $subsite1Page->URLSegment,
282
            "Doesn't allow explicit URLSegment overrides when already existing in same subsite"
283
        );
284
285
        //Change to subsite 2
286
        Subsite::changeSubsite($subsite2->ID);
287
288
        // saving in subsite2 (which already has a page with URLSegment 'contact-us'),
289
        // but linking to a page in subsite1
290
        $subsite2Vp = new SubsitesVirtualPage();
291
        $subsite2Vp->CopyContentFromID = $subsite1Page->ID;
292
        $subsite2Vp->SubsiteID = $subsite2->ID;
293
        $subsite2Vp->write();
294
        $this->assertEquals(
295
            $subsite2Vp->URLSegment,
296
            $subsite1Page->URLSegment,
297
            'Does allow explicit URLSegment overrides when only existing in a different subsite'
298
        );
299
300
        // When changing subsites and re-saving this page, it doesn't trigger a change
301
        Subsite::changeSubsite($subsite1->ID);
302
        $subsite1Page->write();
303
        $subsite2Vp->write();
304
        $this->assertEquals(
305
            $subsite2Vp->URLSegment,
306
            $subsite1Page->URLSegment,
307
            "SubsiteVirtualPage doesn't change urls when being written in another subsite"
308
        );
309
    }
310
311
    protected function fixVersionNumberCache($page)
312
    {
313
        $pages = func_get_args();
314
        foreach ($pages as $p) {
315
            Versioned::prepopulate_versionnumber_cache(SiteTree::class, 'Stage', [$p->ID]);
316
            Versioned::prepopulate_versionnumber_cache(SiteTree::class, 'Live', [$p->ID]);
317
        }
318
    }
319
}
320