annotate core/modules/migrate/tests/src/Kernel/TrackChangesTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@18 1 <?php
Chris@18 2
Chris@18 3 namespace Drupal\Tests\migrate\Kernel;
Chris@18 4
Chris@18 5 /**
Chris@18 6 * Tests migration track changes property.
Chris@18 7 *
Chris@18 8 * @group migrate
Chris@18 9 */
Chris@18 10 class TrackChangesTest extends MigrateTestBase {
Chris@18 11
Chris@18 12 /**
Chris@18 13 * {@inheritdoc}
Chris@18 14 */
Chris@18 15 public static $modules = [
Chris@18 16 'system',
Chris@18 17 'user',
Chris@18 18 'taxonomy',
Chris@18 19 'migrate',
Chris@18 20 'migrate_track_changes_test',
Chris@18 21 'text',
Chris@18 22 ];
Chris@18 23
Chris@18 24 /**
Chris@18 25 * {@inheritdoc}
Chris@18 26 */
Chris@18 27 protected function setUp() {
Chris@18 28 parent::setUp();
Chris@18 29 // Create source test table.
Chris@18 30 $this->sourceDatabase->schema()->createTable('track_changes_term', [
Chris@18 31 'fields' => [
Chris@18 32 'tid' => [
Chris@18 33 'description' => 'Serial',
Chris@18 34 'type' => 'serial',
Chris@18 35 'unsigned' => TRUE,
Chris@18 36 'not null' => TRUE,
Chris@18 37 ],
Chris@18 38 'name' => [
Chris@18 39 'description' => 'Name',
Chris@18 40 'type' => 'varchar',
Chris@18 41 'length' => 128,
Chris@18 42 'not null' => TRUE,
Chris@18 43 'default' => '',
Chris@18 44 ],
Chris@18 45 'description' => [
Chris@18 46 'description' => 'Name',
Chris@18 47 'type' => 'varchar',
Chris@18 48 'length' => 255,
Chris@18 49 'not null' => FALSE,
Chris@18 50 'default' => '',
Chris@18 51 ],
Chris@18 52 ],
Chris@18 53 'primary key' => [
Chris@18 54 'tid',
Chris@18 55 ],
Chris@18 56 'description' => 'Contains taxonomy terms to import',
Chris@18 57 ]);
Chris@18 58
Chris@18 59 // Add 4 items to source table.
Chris@18 60 $this->sourceDatabase->insert('track_changes_term')
Chris@18 61 ->fields([
Chris@18 62 'name',
Chris@18 63 'description',
Chris@18 64 ])
Chris@18 65 ->values([
Chris@18 66 'name' => 'Item 1',
Chris@18 67 'description' => 'Text item 1',
Chris@18 68 ])
Chris@18 69 ->values([
Chris@18 70 'name' => 'Item 2',
Chris@18 71 'description' => 'Text item 2',
Chris@18 72 ])
Chris@18 73 ->values([
Chris@18 74 'name' => 'Item 3',
Chris@18 75 'description' => 'Text item 3',
Chris@18 76 ])
Chris@18 77 ->values([
Chris@18 78 'name' => 'Item 4',
Chris@18 79 'description' => 'Text item 4',
Chris@18 80 ])
Chris@18 81 ->execute();
Chris@18 82
Chris@18 83 $this->installEntitySchema('taxonomy_term');
Chris@18 84 $this->installEntitySchema('user');
Chris@18 85
Chris@18 86 $this->executeMigration('track_changes_test');
Chris@18 87 }
Chris@18 88
Chris@18 89 /**
Chris@18 90 * Tests track changes property of SqlBase.
Chris@18 91 */
Chris@18 92 public function testTrackChanges() {
Chris@18 93 // Assert all of the terms have been imported.
Chris@18 94 $this->assertTermExists('name', 'Item 1');
Chris@18 95 $this->assertTermExists('name', 'Item 2');
Chris@18 96 $this->assertTermExists('description', 'Text item 3');
Chris@18 97 $this->assertTermExists('description', 'Text item 4');
Chris@18 98
Chris@18 99 // Update Item 1 triggering its track_changes by name.
Chris@18 100 $this->sourceDatabase->update('track_changes_term')
Chris@18 101 ->fields([
Chris@18 102 'name' => 'Item 1 updated',
Chris@18 103 ])
Chris@18 104 ->condition('name', 'Item 1')
Chris@18 105 ->execute();
Chris@18 106
Chris@18 107 // Update Item 2 keeping it's track_changes name the same.
Chris@18 108 $this->sourceDatabase->update('track_changes_term')
Chris@18 109 ->fields([
Chris@18 110 'name' => 'Item 2',
Chris@18 111 ])
Chris@18 112 ->condition('name', 'Item 2')
Chris@18 113 ->execute();
Chris@18 114
Chris@18 115 // Update Item 3 triggering its track_changes by field.
Chris@18 116 $this->sourceDatabase->update('track_changes_term')
Chris@18 117 ->fields([
Chris@18 118 'description' => 'Text item 3 updated',
Chris@18 119 ])
Chris@18 120 ->condition('name', 'Item 3')
Chris@18 121 ->execute();
Chris@18 122
Chris@18 123 // Update Item 2 keeping it's track_changes field the same.
Chris@18 124 $this->sourceDatabase->update('track_changes_term')
Chris@18 125 ->fields([
Chris@18 126 'description' => 'Text item 4',
Chris@18 127 ])
Chris@18 128 ->condition('name', 'Item 4')
Chris@18 129 ->execute();
Chris@18 130
Chris@18 131 // Execute migration again.
Chris@18 132 $this->executeMigration('track_changes_test');
Chris@18 133
Chris@18 134 // Item with name changes should be updated.
Chris@18 135 $this->assertTermExists('name', 'Item 1 updated');
Chris@18 136 $this->assertTermDoesNotExist('name', 'Item 1');
Chris@18 137
Chris@18 138 // Item without name changes should not be updated.
Chris@18 139 $this->assertTermExists('name', 'Item 2');
Chris@18 140
Chris@18 141 // Item with field changes should be updated.
Chris@18 142 $this->assertTermExists('description', 'Text item 3 updated');
Chris@18 143 $this->assertTermDoesNotExist('description', 'Text item 3');
Chris@18 144
Chris@18 145 // Item without field changes should not be updated.
Chris@18 146 $this->assertTermExists('description', 'Text item 4');
Chris@18 147 }
Chris@18 148
Chris@18 149 /**
Chris@18 150 * Assert that term with given name exists.
Chris@18 151 *
Chris@18 152 * @param string $property
Chris@18 153 * Property to evaluate.
Chris@18 154 * @param string $value
Chris@18 155 * Value to evaluate.
Chris@18 156 */
Chris@18 157 protected function assertTermExists($property, $value) {
Chris@18 158 self::assertTrue($this->termExists($property, $value));
Chris@18 159 }
Chris@18 160
Chris@18 161 /**
Chris@18 162 * Assert that term with given title does not exist.
Chris@18 163 *
Chris@18 164 * @param string $property
Chris@18 165 * Property to evaluate.
Chris@18 166 * @param string $value
Chris@18 167 * Value to evaluate.
Chris@18 168 */
Chris@18 169 protected function assertTermDoesNotExist($property, $value) {
Chris@18 170 self::assertFalse($this->termExists($property, $value));
Chris@18 171 }
Chris@18 172
Chris@18 173 /**
Chris@18 174 * Checks if term with given name exists.
Chris@18 175 *
Chris@18 176 * @param string $property
Chris@18 177 * Property to evaluate.
Chris@18 178 * @param string $value
Chris@18 179 * Value to evaluate.
Chris@18 180 *
Chris@18 181 * @return bool
Chris@18 182 */
Chris@18 183 protected function termExists($property, $value) {
Chris@18 184 $property = $property === 'description' ? 'description__value' : $property;
Chris@18 185 $query = \Drupal::entityQuery('taxonomy_term');
Chris@18 186 $result = $query
Chris@18 187 ->condition($property, $value)
Chris@18 188 ->range(0, 1)
Chris@18 189 ->execute();
Chris@18 190
Chris@18 191 return !empty($result);
Chris@18 192 }
Chris@18 193
Chris@18 194 }