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