2011-03-18 04:37:22 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
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;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-08-09 04:53:38 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2018-04-06 05:53:57 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2016-08-23 04:36:06 +02:00
|
|
|
|
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
|
2018-04-06 05:53:57 +02:00
|
|
|
{
|
|
|
|
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
|
2018-04-06 05:53:57 +02:00
|
|
|
{
|
|
|
|
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'];
|
|
|
|
$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
|
|
|
|
2018-04-06 05:53:57 +02: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(
|
2018-04-06 05:53:57 +02:00
|
|
|
"Migrated page links on 5 Pages",
|
2017-01-25 21:59:25 +01:00
|
|
|
ob_get_contents(),
|
|
|
|
'Rewritten links are correctly reported'
|
|
|
|
);
|
2018-04-06 05:53:57 +02:00
|
|
|
DB::quiet(true);
|
2017-01-25 21:59:25 +01:00
|
|
|
ob_end_clean();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2018-04-06 05:53:57 +02: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
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
// Ensure all links are created
|
2019-05-28 02:51:23 +02:00
|
|
|
$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
|
|
|
}
|
2012-04-12 09:23:20 +02:00
|
|
|
}
|