silverstripe /
silverstripe-elemental
| 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(): void |
||||
| 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->assertStringContainsString('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
|
|||||
| 58 | $this->assertCount(1, $elements); |
||||
| 59 | |||||
| 60 | /** @var ElementContent&Versioned $contentElement */ |
||||
| 61 | $contentElement = $elements->first(); |
||||
| 62 | $this->assertSame('This is page 3', $contentElement->HTML); |
||||
| 63 | |||||
| 64 | // Assert that the element and page are "live" |
||||
| 65 | $this->assertTrue($contentElement->isLiveVersion()); |
||||
|
0 ignored issues
–
show
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
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->assertStringContainsString('Finished migrating 1 pages\' content', $output); |
||||
| 80 | |||||
| 81 | $page = $this->objFromFixture(TestPage::class, 'page3'); |
||||
| 82 | $this->assertStringContainsString('This is page 3', $page->Content, 'Content is not removed from the page'); |
||||
| 83 | |||||
| 84 | $element = $page->ElementalArea->Elements()->first(); |
||||
| 85 | $this->assertStringContainsString('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->assertStringContainsString('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->assertStringContainsString('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
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
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->assertStringContainsString('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); |
||||
| 140 | |||||
| 141 | $this->assertFalse($page->isLiveVersion()); |
||||
| 142 | $this->assertFalse($element->isLiveVersion()); |
||||
| 143 | |||||
| 144 | $this->assertFalse($element->isPublished()); |
||||
|
0 ignored issues
–
show
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
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->assertStringContainsString('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
|
|||||
| 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
|
|||||
| 169 | $this->assertCount(1, $elements); |
||||
| 170 | |||||
| 171 | /** @var ElementContent&Versioned $contentElement */ |
||||
| 172 | $contentElement = $elements->first(); |
||||
| 173 | $this->assertSame('This is page 3', $contentElement->HTML); |
||||
| 174 | |||||
| 175 | // Assert that the element and page are "live" |
||||
| 176 | $this->assertFalse($contentElement->isLiveVersion()); |
||||
| 177 | $this->assertFalse($page->isLiveVersion()); |
||||
| 178 | } |
||||
| 179 | |||||
| 180 | public function testIgnoredClassesContentIsNotCleared() |
||||
| 181 | { |
||||
| 182 | Config::modify()->set(ElementalPageExtension::class, 'ignored_classes', [TestPage::class]); |
||||
| 183 | $task = new MigrateContentToElement(); |
||||
| 184 | |||||
| 185 | // Ensure the page is published to begin with to meet criteria for being migrated |
||||
| 186 | /** @var TestPage&Versioned $page */ |
||||
| 187 | $page = $this->objFromFixture(TestPage::class, 'page3'); |
||||
| 188 | $page->publishSingle(); |
||||
| 189 | |||||
| 190 | ob_start(); |
||||
| 191 | $task->run(new HTTPRequest('GET', '')); |
||||
| 192 | $output = ob_get_clean(); |
||||
| 193 | |||||
| 194 | $this->assertStringContainsString('Finished migrating 0 pages\' content', $output); |
||||
| 195 | |||||
| 196 | // Get the page and confirm its content has not been altered. |
||||
| 197 | $page = $this->objFromFixture(TestPage::class, 'page3'); |
||||
| 198 | $this->assertSame('This is page 3', $page->Content); |
||||
| 199 | |||||
| 200 | // Check that no elements were created. |
||||
| 201 | /** @var HasManyList $elements */ |
||||
| 202 | $elements = $page->ElementalArea->Elements(); |
||||
| 203 | $this->assertCount(0, $elements); |
||||
| 204 | |||||
| 205 | // Assert that the element is still "live" |
||||
| 206 | $this->assertTrue($page->isLiveVersion()); |
||||
| 207 | } |
||||
| 208 | } |
||||
| 209 |
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.