silverstripe /
silverstripe-cms
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\CMS\Tests\Tasks; |
||
| 4 | |||
| 5 | use SilverStripe\CMS\Model\SiteTree; |
||
| 6 | use SilverStripe\CMS\Tasks\MigrateSiteTreeLinkingTask; |
||
| 7 | use SilverStripe\Dev\SapphireTest; |
||
| 8 | use SilverStripe\ORM\DataObject; |
||
| 9 | use SilverStripe\ORM\DB; |
||
| 10 | |||
| 11 | class MigrateSiteTreeLinkingTaskTest extends SapphireTest |
||
| 12 | { |
||
| 13 | protected static $fixture_file = 'MigrateSiteTreeLinkingTaskTest.yml'; |
||
| 14 | |||
| 15 | protected static $use_draft_site = true; |
||
| 16 | |||
| 17 | public static function setUpBeforeClass() : void |
||
| 18 | { |
||
| 19 | parent::setUpBeforeClass(); |
||
| 20 | |||
| 21 | // Cover db reset in case parent did not start |
||
| 22 | if (!static::getExtraDataObjects()) { |
||
| 23 | DataObject::reset(); |
||
| 24 | static::resetDBSchema(true, true); |
||
| 25 | } |
||
| 26 | |||
| 27 | // Ensure legacy SiteTree_LinkTracking table exists |
||
| 28 | DB::get_schema()->schemaUpdate(function () { |
||
| 29 | DB::require_table('SiteTree_LinkTracking', [ |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 30 | 'SiteTreeID' => 'Int', |
||
| 31 | 'ChildID' => 'Int', |
||
| 32 | 'FieldName' => 'Varchar', |
||
| 33 | ]); |
||
| 34 | }); |
||
| 35 | } |
||
| 36 | |||
| 37 | protected function setUp() : void |
||
| 38 | { |
||
| 39 | parent::setUp(); |
||
| 40 | |||
| 41 | // Manually bootstrap all Content blocks with soft coded IDs (raw sql to avoid save hooks) |
||
| 42 | $replacements = [ |
||
| 43 | '$$ABOUTID$$' => $this->idFromFixture(SiteTree::class, 'about'), |
||
| 44 | '$$HOMEID$$' => $this->idFromFixture(SiteTree::class, 'home'), |
||
| 45 | '$$STAFFID$$' => $this->idFromFixture(SiteTree::class, 'staff'), |
||
| 46 | ]; |
||
| 47 | foreach (DB::query('SELECT "ID", "Content" FROM "SiteTree"') as $row) { |
||
| 48 | $id = (int)$row['ID']; |
||
| 49 | $content = str_replace(array_keys($replacements), array_values($replacements), $row['Content']); |
||
| 50 | DB::prepared_query('UPDATE "SiteTree" SET "Content" = ? WHERE "ID" = ?', [$content, $id]); |
||
| 51 | } |
||
| 52 | DataObject::reset(); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function testLinkingMigration() |
||
| 56 | { |
||
| 57 | ob_start(); |
||
| 58 | |||
| 59 | DB::quiet(false); |
||
| 60 | $task = new MigrateSiteTreeLinkingTask(); |
||
| 61 | $task->run(null); |
||
| 62 | $this->assertStringContainsString( |
||
| 63 | "Migrated page links on 5 Pages", |
||
| 64 | ob_get_contents(), |
||
| 65 | 'Rewritten links are correctly reported' |
||
| 66 | ); |
||
| 67 | DB::quiet(true); |
||
| 68 | ob_end_clean(); |
||
| 69 | |||
| 70 | // Query links for pages |
||
| 71 | /** @var SiteTree $home */ |
||
| 72 | $home = $this->objFromFixture(SiteTree::class, 'home'); |
||
| 73 | /** @var SiteTree $about */ |
||
| 74 | $about = $this->objFromFixture(SiteTree::class, 'about'); |
||
| 75 | /** @var SiteTree $staff */ |
||
| 76 | $staff = $this->objFromFixture(SiteTree::class, 'staff'); |
||
| 77 | /** @var SiteTree $action */ |
||
| 78 | $action = $this->objFromFixture(SiteTree::class, 'action'); |
||
| 79 | /** @var SiteTree $hash */ |
||
| 80 | $hash = $this->objFromFixture(SiteTree::class, 'hash_link'); |
||
| 81 | |||
| 82 | // Ensure all links are created |
||
| 83 | $this->assertListEquals([['ID' => $about->ID], ['ID' => $staff->ID]], $home->LinkTracking()); |
||
| 84 | $this->assertListEquals([['ID' => $home->ID], ['ID' => $staff->ID]], $about->LinkTracking()); |
||
| 85 | $this->assertListEquals([['ID' => $home->ID], ['ID' => $about->ID]], $staff->LinkTracking()); |
||
| 86 | $this->assertListEquals([['ID' => $home->ID]], $action->LinkTracking()); |
||
| 87 | $this->assertListEquals([['ID' => $home->ID], ['ID' => $about->ID]], $hash->LinkTracking()); |
||
| 88 | } |
||
| 89 | } |
||
| 90 |