Chris@18: sourceDatabase->schema()->createTable('track_changes_term', [ Chris@18: 'fields' => [ Chris@18: 'tid' => [ Chris@18: 'description' => 'Serial', Chris@18: 'type' => 'serial', Chris@18: 'unsigned' => TRUE, Chris@18: 'not null' => TRUE, Chris@18: ], Chris@18: 'name' => [ Chris@18: 'description' => 'Name', Chris@18: 'type' => 'varchar', Chris@18: 'length' => 128, Chris@18: 'not null' => TRUE, Chris@18: 'default' => '', Chris@18: ], Chris@18: 'description' => [ Chris@18: 'description' => 'Name', Chris@18: 'type' => 'varchar', Chris@18: 'length' => 255, Chris@18: 'not null' => FALSE, Chris@18: 'default' => '', Chris@18: ], Chris@18: ], Chris@18: 'primary key' => [ Chris@18: 'tid', Chris@18: ], Chris@18: 'description' => 'Contains taxonomy terms to import', Chris@18: ]); Chris@18: Chris@18: // Add 4 items to source table. Chris@18: $this->sourceDatabase->insert('track_changes_term') Chris@18: ->fields([ Chris@18: 'name', Chris@18: 'description', Chris@18: ]) Chris@18: ->values([ Chris@18: 'name' => 'Item 1', Chris@18: 'description' => 'Text item 1', Chris@18: ]) Chris@18: ->values([ Chris@18: 'name' => 'Item 2', Chris@18: 'description' => 'Text item 2', Chris@18: ]) Chris@18: ->values([ Chris@18: 'name' => 'Item 3', Chris@18: 'description' => 'Text item 3', Chris@18: ]) Chris@18: ->values([ Chris@18: 'name' => 'Item 4', Chris@18: 'description' => 'Text item 4', Chris@18: ]) Chris@18: ->execute(); Chris@18: Chris@18: $this->installEntitySchema('taxonomy_term'); Chris@18: $this->installEntitySchema('user'); Chris@18: Chris@18: $this->executeMigration('track_changes_test'); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Tests track changes property of SqlBase. Chris@18: */ Chris@18: public function testTrackChanges() { Chris@18: // Assert all of the terms have been imported. Chris@18: $this->assertTermExists('name', 'Item 1'); Chris@18: $this->assertTermExists('name', 'Item 2'); Chris@18: $this->assertTermExists('description', 'Text item 3'); Chris@18: $this->assertTermExists('description', 'Text item 4'); Chris@18: Chris@18: // Update Item 1 triggering its track_changes by name. Chris@18: $this->sourceDatabase->update('track_changes_term') Chris@18: ->fields([ Chris@18: 'name' => 'Item 1 updated', Chris@18: ]) Chris@18: ->condition('name', 'Item 1') Chris@18: ->execute(); Chris@18: Chris@18: // Update Item 2 keeping it's track_changes name the same. Chris@18: $this->sourceDatabase->update('track_changes_term') Chris@18: ->fields([ Chris@18: 'name' => 'Item 2', Chris@18: ]) Chris@18: ->condition('name', 'Item 2') Chris@18: ->execute(); Chris@18: Chris@18: // Update Item 3 triggering its track_changes by field. Chris@18: $this->sourceDatabase->update('track_changes_term') Chris@18: ->fields([ Chris@18: 'description' => 'Text item 3 updated', Chris@18: ]) Chris@18: ->condition('name', 'Item 3') Chris@18: ->execute(); Chris@18: Chris@18: // Update Item 2 keeping it's track_changes field the same. Chris@18: $this->sourceDatabase->update('track_changes_term') Chris@18: ->fields([ Chris@18: 'description' => 'Text item 4', Chris@18: ]) Chris@18: ->condition('name', 'Item 4') Chris@18: ->execute(); Chris@18: Chris@18: // Execute migration again. Chris@18: $this->executeMigration('track_changes_test'); Chris@18: Chris@18: // Item with name changes should be updated. Chris@18: $this->assertTermExists('name', 'Item 1 updated'); Chris@18: $this->assertTermDoesNotExist('name', 'Item 1'); Chris@18: Chris@18: // Item without name changes should not be updated. Chris@18: $this->assertTermExists('name', 'Item 2'); Chris@18: Chris@18: // Item with field changes should be updated. Chris@18: $this->assertTermExists('description', 'Text item 3 updated'); Chris@18: $this->assertTermDoesNotExist('description', 'Text item 3'); Chris@18: Chris@18: // Item without field changes should not be updated. Chris@18: $this->assertTermExists('description', 'Text item 4'); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Assert that term with given name exists. Chris@18: * Chris@18: * @param string $property Chris@18: * Property to evaluate. Chris@18: * @param string $value Chris@18: * Value to evaluate. Chris@18: */ Chris@18: protected function assertTermExists($property, $value) { Chris@18: self::assertTrue($this->termExists($property, $value)); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Assert that term with given title does not exist. Chris@18: * Chris@18: * @param string $property Chris@18: * Property to evaluate. Chris@18: * @param string $value Chris@18: * Value to evaluate. Chris@18: */ Chris@18: protected function assertTermDoesNotExist($property, $value) { Chris@18: self::assertFalse($this->termExists($property, $value)); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Checks if term with given name exists. Chris@18: * Chris@18: * @param string $property Chris@18: * Property to evaluate. Chris@18: * @param string $value Chris@18: * Value to evaluate. Chris@18: * Chris@18: * @return bool Chris@18: */ Chris@18: protected function termExists($property, $value) { Chris@18: $property = $property === 'description' ? 'description__value' : $property; Chris@18: $query = \Drupal::entityQuery('taxonomy_term'); Chris@18: $result = $query Chris@18: ->condition($property, $value) Chris@18: ->range(0, 1) Chris@18: ->execute(); Chris@18: Chris@18: return !empty($result); Chris@18: } Chris@18: Chris@18: }