Passed
Pull Request — master (#652)
by Guy
03:36
created

MigrateContentToElementTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 124
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testPublishingConfigurationIsRespected() 0 32 1
A testContentIsNotClearedWhenConfigured() 0 26 1
A testContentIsMigratedFromPagesToNewElements() 0 27 1
A testTargetElementConfigurationIsRespected() 0 18 1
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
    public function testContentIsMigratedFromPagesToNewElements()
31
    {
32
        $task = new MigrateContentToElement();
33
34
        ob_start();
35
        $task->run(new HTTPRequest('GET', ''));
36
        $output = ob_get_clean();
37
38
        $this->assertContains('Finished migrating 1 pages\' content', $output);
39
40
        // Get the page that should've been updated and the content should be removed
41
        /** @var TestPage&Versioned $page */
42
        $page = $this->objFromFixture(TestPage::class, 'page3');
43
        $this->assertEmpty($page->Content);
0 ignored issues
show
Bug introduced by
The property Content does not seem to exist on SilverStripe\Versioned\Versioned.
Loading history...
44
45
        // Check that there's one element and it contains the old content
46
        /** @var HasManyList $elements */
47
        $elements = $page->ElementalArea->Elements();
0 ignored issues
show
Bug introduced by
The property ElementalArea does not seem to exist on SilverStripe\Versioned\Versioned.
Loading history...
48
        $this->assertCount(1, $elements);
49
50
        /** @var ElementContent&Versioned $contentElement */
51
        $contentElement = $elements->first();
52
        $this->assertSame('This is page 3', $contentElement->HTML);
0 ignored issues
show
Bug Best Practice introduced by
The property HTML does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __get, consider adding a @property annotation.
Loading history...
53
54
        // Assert that the element and page are "live"
55
        $this->assertTrue($contentElement->isLiveVersion());
0 ignored issues
show
Bug introduced by
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

55
        $this->assertTrue($contentElement->/** @scrutinizer ignore-call */ isLiveVersion());
Loading history...
56
        $this->assertTrue($page->isLiveVersion());
57
    }
58
59
    public function testContentIsNotClearedWhenConfigured()
60
    {
61
        Config::modify()->set(MigrateContentToElement::class, 'clear_content', false);
62
63
        $task = new MigrateContentToElement();
64
65
        ob_start();
66
        $task->run(new HTTPRequest('GET', ''));
67
        $output = ob_get_clean();
68
69
        $this->assertContains('Finished migrating 1 pages\' content', $output);
70
71
        $page = $this->objFromFixture(TestPage::class, 'page3');
72
        $this->assertContains('This is page 3', $page->Content, 'Content is not removed from the page');
73
74
        $element = $page->ElementalArea->Elements()->first();
0 ignored issues
show
Bug introduced by
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

74
        $element = $page->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...
75
        $this->assertContains('This is page 3', $element->HTML, 'Content is still added to a new element');
76
77
        // Run the task again and assert the page is not picked up again
78
        ob_start();
79
        $task->run(new HTTPRequest('GET', ''));
80
        $output = ob_get_clean();
81
82
        $this->assertContains('Finished migrating 0 pages\' content', $output);
83
        $page = $this->objFromFixture(TestPage::class, 'page3');
84
        $this->assertCount(1, $page->ElementalArea->Elements());
85
    }
86
87
    public function testTargetElementConfigurationIsRespected()
88
    {
89
        Config::modify()->set(MigrateContentToElement::class, 'target_element', TestElement::class);
90
        Config::modify()->set(MigrateContentToElement::class, 'target_element_field', 'TestValue');
91
92
        $task = new MigrateContentToElement();
93
94
        ob_start();
95
        $task->run(new HTTPRequest('GET', ''));
96
        $output = ob_get_clean();
97
98
        $this->assertContains('Finished migrating 1 pages\' content', $output);
99
100
        // Get the page that should've been updated and the content should be removed
101
        $element = $this->objFromFixture(TestPage::class, 'page3')->ElementalArea->Elements()->first();
0 ignored issues
show
Bug introduced by
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

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

134
        $this->assertFalse($element->/** @scrutinizer ignore-call */ isPublished());
Loading history...
135
        $this->assertEmpty($page->Content);
136
137
        $livePage = Versioned::get_by_stage(TestPage::class, Versioned::LIVE)->byID($page->ID);
138
        $this->assertSame('This is page 3', $livePage->Content);
139
    }
140
}
141