annotate core/modules/migrate/tests/src/Kernel/TrackChangesTest.php @ 5:12f9dff5fda9 tip

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