Passed
Push — master ( a1de92...1a465b )
by Robbie
03:52
created

testContentIsNotClearedWhenConfigured()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 26
rs 9.7333
c 0
b 0
f 0
1
<?php
2
namespace DNADesign\Elemental\Tests\Tasks;
3
4
use DNADesign\Elemental\Extensions\ElementalPageExtension;
5
use DNADesign\Elemental\Models\ElementContent;
6
use DNADesign\Elemental\Tasks\MigrateContentToElement;
7
use DNADesign\Elemental\Tests\Src\TestElement;
8
use DNADesign\Elemental\Tests\Src\TestPage;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Dev\SapphireTest;
12
use SilverStripe\ORM\HasManyList;
13
use SilverStripe\Versioned\Versioned;
14
15
class MigrateContentToElementTest extends SapphireTest
16
{
17
    protected static $fixture_file = 'MigrateContentToElementTest.yml';
18
19
    protected static $required_extensions = [
20
        TestPage::class => [
21
            ElementalPageExtension::class,
22
        ],
23
    ];
24
25
    protected static $extra_dataobjects = [
26
        TestElement::class,
27
        TestPage::class,
28
    ];
29
30
    protected function setUp()
31
    {
32
        TestPage::create()->flushCache();
33
        parent::setUp();
34
    }
35
36
    public function testContentIsMigratedFromPagesToNewElements()
37
    {
38
        $task = new MigrateContentToElement();
39
40
        // Ensure the page that should be edited is published to begin with
41
        /** @var TestPage&Versioned $page */
42
        $page = $this->objFromFixture(TestPage::class, 'page3');
43
        $page->publishSingle();
44
45
        ob_start();
46
        $task->run(new HTTPRequest('GET', ''));
47
        $output = ob_get_clean();
48
49
        $this->assertContains('Finished migrating 1 pages\' content', $output);
50
51
        // Get the page that should've been updated and the content should be removed
52
        $page = $this->objFromFixture(TestPage::class, 'page3');
53
        $this->assertEmpty($page->Content);
54
55
        // Check that there's one element and it contains the old content
56
        /** @var HasManyList $elements */
57
        $elements = $page->ElementalArea->Elements();
0 ignored issues
show
Bug introduced by Guy Marriott
The method Elements() does not exist on null. ( Ignorable by Annotation )

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

57
        /** @scrutinizer ignore-call */ 
58
        $elements = $page->ElementalArea->Elements();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
        $this->assertCount(1, $elements);
59
60
        /** @var ElementContent&Versioned $contentElement */
61
        $contentElement = $elements->first();
62
        $this->assertSame('This is page 3', $contentElement->HTML);
0 ignored issues
show
Bug Best Practice introduced by Guy Marriott
The property HTML does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __get, consider adding a @property annotation.
Loading history...
63
64
        // Assert that the element and page are "live"
65
        $this->assertTrue($contentElement->isLiveVersion());
0 ignored issues
show
Bug introduced by Guy Marriott
The method isLiveVersion() does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

65
        $this->assertTrue($contentElement->/** @scrutinizer ignore-call */ isLiveVersion());
Loading history...
66
        $this->assertTrue($page->isLiveVersion());
67
    }
68
69
    public function testContentIsNotClearedWhenConfigured()
70
    {
71
        Config::modify()->set(MigrateContentToElement::class, 'clear_content', false);
72
73
        $task = new MigrateContentToElement();
74
75
        ob_start();
76
        $task->run(new HTTPRequest('GET', ''));
77
        $output = ob_get_clean();
78
79
        $this->assertContains('Finished migrating 1 pages\' content', $output);
80
81
        $page = $this->objFromFixture(TestPage::class, 'page3');
82
        $this->assertContains('This is page 3', $page->Content, 'Content is not removed from the page');
83
84
        $element = $page->ElementalArea->Elements()->first();
85
        $this->assertContains('This is page 3', $element->HTML, 'Content is still added to a new element');
86
87
        // Run the task again and assert the page is not picked up again
88
        ob_start();
89
        $task->run(new HTTPRequest('GET', ''));
90
        $output = ob_get_clean();
91
92
        $this->assertContains('Finished migrating 0 pages\' content', $output);
93
        $page = $this->objFromFixture(TestPage::class, 'page3');
94
        $this->assertCount(1, $page->ElementalArea->Elements());
95
    }
96
97
    public function testTargetElementConfigurationIsRespected()
98
    {
99
        Config::modify()->set(MigrateContentToElement::class, 'target_element', TestElement::class);
100
        Config::modify()->set(MigrateContentToElement::class, 'target_element_field', 'TestValue');
101
102
        $task = new MigrateContentToElement();
103
104
        ob_start();
105
        $task->run(new HTTPRequest('GET', ''));
106
        $output = ob_get_clean();
107
108
        $this->assertContains('Finished migrating 1 pages\' content', $output);
109
110
        // Get the page that should've been updated and the content should be removed
111
        $element = $this->objFromFixture(TestPage::class, 'page3')->ElementalArea->Elements()->first();
0 ignored issues
show
Bug introduced by Guy Marriott
The method Elements() does not exist on null. ( Ignorable by Annotation )

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

111
        $element = $this->objFromFixture(TestPage::class, 'page3')->ElementalArea->/** @scrutinizer ignore-call */ Elements()->first();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
113
        $this->assertInstanceOf(TestElement::class, $element);
114
        $this->assertSame('This is page 3', $element->TestValue);
115
    }
116
117
    public function testPublishingConfigurationIsRespected()
118
    {
119
        Config::modify()->set(MigrateContentToElement::class, 'publish_changes', false);
120
121
        // Ensure the page is published to begin with
122
        /** @var TestPage&Versioned $page */
123
        $page = $this->objFromFixture(TestPage::class, 'page3');
124
        $page->publishSingle();
125
126
        $task = new MigrateContentToElement();
127
128
        ob_start();
129
        $task->run(new HTTPRequest('GET', ''));
130
        $output = ob_get_clean();
131
132
        $this->assertContains('Finished migrating 1 pages\' content', $output);
133
134
        // Get the page that should've been updated and the content should be removed
135
        $page = $this->objFromFixture(TestPage::class, 'page3');
136
        /** @var ElementContent&Versioned $element */
137
        $element = $page->ElementalArea->Elements()->first();
138
139
        $this->assertSame('This is page 3', $element->HTML);
0 ignored issues
show
Bug Best Practice introduced by Guy Marriott
The property HTML does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __get, consider adding a @property annotation.
Loading history...
140
141
        $this->assertFalse($page->isLiveVersion());
142
        $this->assertFalse($element->isLiveVersion());
143
144
        $this->assertFalse($element->isPublished());
0 ignored issues
show
Bug introduced by Guy Marriott
The method isPublished() does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

144
        $this->assertFalse($element->/** @scrutinizer ignore-call */ isPublished());
Loading history...
145
        $this->assertEmpty($page->Content);
146
147
        $livePage = Versioned::get_by_stage(TestPage::class, Versioned::LIVE)->byID($page->ID);
148
        $this->assertSame('This is page 3', $livePage->Content);
149
    }
150
151
    public function testPagesThatWereNotPublishedAreNotPublishedDuringThisTask()
152
    {
153
        $task = new MigrateContentToElement();
154
155
        ob_start();
156
        $task->run(new HTTPRequest('GET', ''));
157
        $output = ob_get_clean();
158
159
        $this->assertContains('Finished migrating 1 pages\' content', $output);
160
161
        // Get the page that should've been updated and the content should be removed
162
        /** @var TestPage&Versioned $page */
163
        $page = $this->objFromFixture(TestPage::class, 'page3');
164
        $this->assertEmpty($page->Content);
0 ignored issues
show
Bug introduced by Guy Marriott
The property Content does not seem to exist on SilverStripe\Versioned\Versioned.
Loading history...
165
166
        // Check that there's one element and it contains the old content
167
        /** @var HasManyList $elements */
168
        $elements = $page->ElementalArea->Elements();
0 ignored issues
show
Bug introduced by Guy Marriott
The property ElementalArea does not seem to exist on SilverStripe\Versioned\Versioned.
Loading history...
169
        $this->assertCount(1, $elements);
170
171
        /** @var ElementContent&Versioned $contentElement */
172
        $contentElement = $elements->first();
173
        $this->assertSame('This is page 3', $contentElement->HTML);
0 ignored issues
show
Bug Best Practice introduced by Guy Marriott
The property HTML does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __get, consider adding a @property annotation.
Loading history...
174
175
        // Assert that the element and page are "live"
176
        $this->assertFalse($contentElement->isLiveVersion());
177
        $this->assertFalse($page->isLiveVersion());
178
    }
179
}
180