View source
<?php
namespace Drupal\Tests\migrate\Kernel;
class HighWaterTest extends MigrateTestBase {
protected static $modules = [
'system',
'user',
'node',
'migrate',
'migrate_high_water_test',
'field',
];
protected function setUp() : void {
parent::setUp();
$this->sourceDatabase
->schema()
->createTable('high_water_node', [
'fields' => [
'id' => [
'description' => 'Serial',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'changed' => [
'description' => 'Highwater',
'type' => 'int',
'unsigned' => TRUE,
],
'title' => [
'description' => 'Title',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
],
],
'primary key' => [
'id',
],
'description' => 'Contains nodes to import',
]);
$this->sourceDatabase
->insert('high_water_node')
->fields([
'title',
'changed',
])
->values([
'title' => 'Item 1',
'changed' => 1,
])
->values([
'title' => 'Item 2',
'changed' => 2,
])
->values([
'title' => 'Item 3',
'changed' => 3,
])
->execute();
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('node', 'node_access');
$this
->executeMigration('high_water_test');
}
public function testHighWater() {
$this
->assertNodeExists('Item 1');
$this
->assertNodeExists('Item 2');
$this
->assertNodeExists('Item 3');
$this->sourceDatabase
->update('high_water_node')
->fields([
'title' => 'Item 1 updated',
'changed' => 2,
])
->condition('title', 'Item 1')
->execute();
$this->sourceDatabase
->update('high_water_node')
->fields([
'title' => 'Item 2 updated',
'changed' => 3,
])
->condition('title', 'Item 2')
->execute();
$this->sourceDatabase
->update('high_water_node')
->fields([
'title' => 'Item 3 updated',
'changed' => 4,
])
->condition('title', 'Item 3')
->execute();
$this
->executeMigration('high_water_test');
$this
->assertNodeExists('Item 1');
$this
->assertNodeDoesNotExist('Item 1 updated');
$this
->assertNodeExists('Item 2');
$this
->assertNodeDoesNotExist('Item 2 updated');
$this
->assertNodeExists('Item 3 updated');
$this
->assertNodeDoesNotExist('Item 3');
}
public function testZeroHighwater() {
$this
->assertNodeExists('Item 1');
$this
->assertNodeExists('Item 2');
$this
->assertNodeExists('Item 3');
$migration = $this->container
->get('plugin.manager.migration')
->CreateInstance('high_water_test', []);
$source = $migration
->getSourcePlugin();
$source
->rewind();
$count = 0;
while ($source
->valid()) {
$count++;
$source
->next();
}
$this
->assertSame(0, $count);
$this->container
->get('keyvalue')
->get('migrate:high_water')
->set('high_water_test', 0);
$migration = $this->container
->get('plugin.manager.migration')
->CreateInstance('high_water_test', []);
$source = $migration
->getSourcePlugin();
$source
->rewind();
$count = 0;
while ($source
->valid()) {
$count++;
$source
->next();
}
$this
->assertSame(3, $count);
}
public function testNullHighwater() {
$this
->assertNodeExists('Item 1');
$this
->assertNodeExists('Item 2');
$this
->assertNodeExists('Item 3');
$migration = $this->container
->get('plugin.manager.migration')
->CreateInstance('high_water_test', []);
$source = $migration
->getSourcePlugin();
$source
->rewind();
$count = 0;
while ($source
->valid()) {
$count++;
$source
->next();
}
$this
->assertSame(0, $count);
$this->container
->get('keyvalue')
->get('migrate:high_water')
->delete('high_water_test');
$migration = $this->container
->get('plugin.manager.migration')
->CreateInstance('high_water_test', []);
$source = $migration
->getSourcePlugin();
$source
->rewind();
$count = 0;
while ($source
->valid()) {
$count++;
$source
->next();
}
$this
->assertSame(3, $count);
}
public function testHighWaterUpdate() {
$this
->assertNodeExists('Item 1');
$this
->assertNodeExists('Item 2');
$this
->assertNodeExists('Item 3');
$this->sourceDatabase
->update('high_water_node')
->fields([
'title' => 'Item 1 updated',
'changed' => 2,
])
->condition('title', 'Item 1')
->execute();
$this->sourceDatabase
->update('high_water_node')
->fields([
'title' => 'Item 2 updated',
'changed' => 3,
])
->condition('title', 'Item 2')
->execute();
$this->sourceDatabase
->update('high_water_node')
->fields([
'title' => 'Item 3 updated',
'changed' => 4,
])
->condition('title', 'Item 3')
->execute();
$id_map = $this
->getMigration('high_water_test')
->getIdMap();
$id_map
->prepareUpdate();
$this
->executeMigration('high_water_test');
$this
->assertNodeExists('Item 1 updated');
$this
->assertNodeDoesNotExist('Item 1');
$this
->assertNodeExists('Item 2 updated');
$this
->assertNodeDoesNotExist('Item 2');
$this
->assertNodeExists('Item 3 updated');
$this
->assertNodeDoesNotExist('Item 3');
}
protected function assertNodeExists($title) {
self::assertTrue($this
->nodeExists($title));
}
protected function assertNodeDoesNotExist($title) {
self::assertFalse($this
->nodeExists($title));
}
protected function nodeExists($title) {
$query = \Drupal::entityQuery('node')
->accessCheck(FALSE);
$result = $query
->condition('title', $title)
->range(0, 1)
->execute();
return !empty($result);
}
}