silverstripe-cms/tests/php/Tasks/MigrateSiteTreeLinkingTaskTest.php

90 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2017-08-09 04:53:38 +02:00
namespace SilverStripe\CMS\Tests\Tasks;
2017-08-09 03:25:12 +02:00
2017-08-09 04:53:38 +02:00
use SilverStripe\CMS\Model\SiteTree;
2016-08-10 06:08:39 +02:00
use SilverStripe\CMS\Tasks\MigrateSiteTreeLinkingTask;
use SilverStripe\Dev\SapphireTest;
2017-08-09 04:53:38 +02:00
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
2017-01-25 21:59:25 +01:00
class MigrateSiteTreeLinkingTaskTest extends SapphireTest
{
protected static $fixture_file = 'MigrateSiteTreeLinkingTaskTest.yml';
2016-03-08 21:50:55 +01:00
2017-01-25 21:59:25 +01:00
protected static $use_draft_site = true;
2016-03-08 21:50:55 +01:00
2021-10-27 23:40:52 +02:00
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
// Cover db reset in case parent did not start
if (!static::getExtraDataObjects()) {
DataObject::reset();
static::resetDBSchema(true, true);
}
// Ensure legacy SiteTree_LinkTracking table exists
DB::get_schema()->schemaUpdate(function () {
DB::require_table('SiteTree_LinkTracking', [
'SiteTreeID' => 'Int',
'ChildID' => 'Int',
'FieldName' => 'Varchar',
]);
});
}
2021-10-27 23:40:52 +02:00
protected function setUp(): void
{
parent::setUp();
// Manually bootstrap all Content blocks with soft coded IDs (raw sql to avoid save hooks)
$replacements = [
'$$ABOUTID$$' => $this->idFromFixture(SiteTree::class, 'about'),
'$$HOMEID$$' => $this->idFromFixture(SiteTree::class, 'home'),
'$$STAFFID$$' => $this->idFromFixture(SiteTree::class, 'staff'),
];
foreach (DB::query('SELECT "ID", "Content" FROM "SiteTree"') as $row) {
$id = (int)$row['ID'];
2022-04-13 07:07:59 +02:00
$content = str_replace(array_keys($replacements ?? []), array_values($replacements ?? []), $row['Content'] ?? '');
DB::prepared_query('UPDATE "SiteTree" SET "Content" = ? WHERE "ID" = ?', [$content, $id]);
}
DataObject::reset();
}
2017-01-25 21:59:25 +01:00
public function testLinkingMigration()
{
ob_start();
2016-03-08 21:50:55 +01:00
DB::quiet(false);
2017-01-25 21:59:25 +01:00
$task = new MigrateSiteTreeLinkingTask();
$task->run(null);
2021-10-27 23:40:52 +02:00
$this->assertStringContainsString(
"Migrated page links on 5 Pages",
2017-01-25 21:59:25 +01:00
ob_get_contents(),
'Rewritten links are correctly reported'
);
DB::quiet(true);
2017-01-25 21:59:25 +01:00
ob_end_clean();
2016-03-08 21:50:55 +01:00
// Query links for pages
/** @var SiteTree $home */
$home = $this->objFromFixture(SiteTree::class, 'home');
/** @var SiteTree $about */
$about = $this->objFromFixture(SiteTree::class, 'about');
/** @var SiteTree $staff */
$staff = $this->objFromFixture(SiteTree::class, 'staff');
/** @var SiteTree $action */
$action = $this->objFromFixture(SiteTree::class, 'action');
/** @var SiteTree $hash */
$hash = $this->objFromFixture(SiteTree::class, 'hash_link');
2016-03-08 21:50:55 +01:00
// Ensure all links are created
$this->assertListEquals([['ID' => $about->ID], ['ID' => $staff->ID]], $home->LinkTracking());
$this->assertListEquals([['ID' => $home->ID], ['ID' => $staff->ID]], $about->LinkTracking());
$this->assertListEquals([['ID' => $home->ID], ['ID' => $about->ID]], $staff->LinkTracking());
$this->assertListEquals([['ID' => $home->ID]], $action->LinkTracking());
$this->assertListEquals([['ID' => $home->ID], ['ID' => $about->ID]], $hash->LinkTracking());
2017-01-25 21:59:25 +01:00
}
}