44
55namespace Drupal \Tests \stanford_events_importer \Unit \EventSubscriber ;
66
7+ use Drupal \Core \Database \Connection ;
78use Drupal \Core \Database \Query \Select ;
89use Drupal \Core \Database \StatementInterface ;
910use Drupal \Core \Entity \EntityStorageInterface ;
1011use Drupal \Core \Entity \EntityTypeManagerInterface ;
11- use Drupal \Core \Entity \Query \QueryInterface ;
1212use Drupal \Core \Field \FieldItemListInterface ;
1313use Drupal \Core \Queue \QueueFactory ;
1414use Drupal \Core \Queue \QueueInterface ;
@@ -38,12 +38,19 @@ class EventsImporterSubscriberTest extends UnitTestCase {
3838 protected $ queueFactory ;
3939
4040 /**
41- * The database connection mock.
41+ * The entity type manager mock.
4242 *
4343 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
4444 */
4545 protected $ entityTypeManager ;
4646
47+ /**
48+ * The database connection mock.
49+ *
50+ * @var \Drupal\Core\Database\Connection|\PHPUnit\Framework\MockObject\MockObject
51+ */
52+ protected $ database ;
53+
4754 /**
4855 * The event subscriber.
4956 *
@@ -59,10 +66,12 @@ protected function setUp(): void {
5966
6067 $ this ->queueFactory = $ this ->createMock (QueueFactory::class);
6168 $ this ->entityTypeManager = $ this ->createMock (EntityTypeManagerInterface::class);
69+ $ this ->database = $ this ->createMock (Connection::class);
6270
6371 $ this ->subscriber = new EventsImporterSubscriber (
6472 $ this ->queueFactory ,
65- $ this ->entityTypeManager
73+ $ this ->entityTypeManager ,
74+ $ this ->database
6675 );
6776 }
6877
@@ -140,84 +149,102 @@ public function testPostImportNonEmptyQueue(): void {
140149 * Tests postImport processes ignored events.
141150 */
142151 public function testPostImportProcessesIgnoredEvents (): void {
152+ $ idMap = $ this ->createMock (Sql::class);
153+ $ idMap ->expects ($ this ->once ())
154+ ->method ('getQualifiedMapTableName ' )
155+ ->willReturn ('migrate_map_stanford_localist_importer ' );
156+
143157 $ migration = $ this ->createMock (MigrationInterface::class);
144158 $ migration ->expects ($ this ->once ())
145159 ->method ('id ' )
146160 ->willReturn ('stanford_localist_importer ' );
161+ $ migration ->expects ($ this ->once ())
162+ ->method ('getIdMap ' )
163+ ->willReturn ($ idMap );
147164
148165 $ event = $ this ->createMock (MigrateImportEvent::class);
149- $ event ->expects ($ this ->once ())
166+ $ event ->expects ($ this ->any ())
150167 ->method ('getMigration ' )
151168 ->willReturn ($ migration );
152169
153170 $ queue = $ this ->createMock (QueueInterface::class);
154171 $ queue ->expects ($ this ->once ())
155172 ->method ('numberOfItems ' )
156173 ->willReturn (0 );
157-
158- $ ignoredItems = [
159- '12345 ' => '67890 ' ,
160- '23456 ' => '78901 ' ,
161- '34567 ' => '89012 ' ,
162- ];
163-
164- $ queue ->expects ($ this ->once ())
174+ $ queue ->expects ($ this ->exactly (2 ))
165175 ->method ('createItem ' )
166- ->willReturnCallback (function ($ item ) use (&$ ignoredItems ) {
167- $ sourceId = (string ) $ item [0 ];
168- $ this ->assertArrayHasKey ($ sourceId , $ ignoredItems );
169- $ this ->assertEquals ((int ) $ ignoredItems [$ sourceId ], $ item [1 ]);
170- return TRUE ;
171- });
176+ ->willReturn (TRUE );
172177
173- $ this ->queueFactory ->expects ($ this ->once ())
178+ $ this ->queueFactory ->expects ($ this ->any ())
174179 ->method ('get ' )
175180 ->with ('localist_event_checker ' )
176181 ->willReturn ($ queue );
177182
178- $ query = $ this ->createMock (QueryInterface::class);
179-
180- $ query ->expects ($ this ->once ())
181- ->method ('accessCheck ' )
182- ->with (FALSE )
183+ $ statement = $ this ->createMock (StatementInterface::class);
184+ $ statement ->expects ($ this ->exactly (3 ))
185+ ->method ('fetchAssoc ' )
186+ ->willReturnOnConsecutiveCalls (
187+ ['destid1 ' => '100 ' , 'sourceid1 ' => '12345 ' ],
188+ ['destid1 ' => '200 ' , 'sourceid1 ' => '23456 ' ],
189+ FALSE
190+ );
191+
192+ $ dbQuery = $ this ->createMock (Select::class);
193+ $ dbQuery ->expects ($ this ->once ())
194+ ->method ('fields ' )
195+ ->with ('map ' , ['destid1 ' , 'sourceid1 ' ])
183196 ->willReturnSelf ();
184-
185- $ query ->expects ($ this ->once ())
197+ $ dbQuery ->expects ($ this ->once ())
186198 ->method ('condition ' )
187- ->with ('su_event_localist_id ' , 0 , ' > ' )
199+ ->with ('source_row_status ' , MigrateIdMapInterface:: STATUS_IGNORED )
188200 ->willReturnSelf ();
189-
190- $ query ->expects ($ this ->once ())
191- ->method ('range ' )
192- ->with (0 , 50 )
201+ $ dbQuery ->expects ($ this ->once ())
202+ ->method ('orderBy ' )
203+ ->with ('last_imported ' , 'ASC ' )
193204 ->willReturnSelf ();
205+ $ dbQuery ->expects ($ this ->once ())
206+ ->method ('execute ' )
207+ ->willReturn ($ statement );
194208
195- $ query ->expects ($ this ->once ())->method ('execute ' )->willReturn ([
196- 1 => 2 ,
197- 3 => 4 ,
198- ]);
199-
200- $ storage = $ this ->createMock (EntityStorageInterface::class);
201- $ storage ->expects ($ this ->once ())->method ('getQuery ' )
202- ->willReturn ($ query );
209+ $ this ->database ->expects ($ this ->once ())
210+ ->method ('select ' )
211+ ->with ('migrate_map_stanford_localist_importer ' , 'map ' )
212+ ->willReturn ($ dbQuery );
203213
204214 $ field = $ this ->createMock (FieldItemListInterface::class);
205- $ field ->expects ($ this ->once ())->method ('getString ' )->willReturn (12345 );
215+ $ field ->expects ($ this ->exactly (2 ))
216+ ->method ('getString ' )
217+ ->willReturnOnConsecutiveCalls ('67890 ' , '78901 ' );
218+ $ field ->expects ($ this ->exactly (2 ))
219+ ->method ('count ' )
220+ ->willReturn (1 );
221+
222+ $ node1 = $ this ->createMock (NodeInterface::class);
223+ $ node1 ->expects ($ this ->once ())
224+ ->method ('hasField ' )
225+ ->with ('su_event_localist_id ' )
226+ ->willReturn (TRUE );
227+ $ node1 ->expects ($ this ->exactly (2 ))
228+ ->method ('get ' )
229+ ->with ('su_event_localist_id ' )
230+ ->willReturn ($ field );
206231
207- $ node = $ this ->createMock (NodeInterface::class);
208- $ node ->expects ($ this ->once ())
232+ $ node2 = $ this ->createMock (NodeInterface::class);
233+ $ node2 ->expects ($ this ->once ())
234+ ->method ('hasField ' )
235+ ->with ('su_event_localist_id ' )
236+ ->willReturn (TRUE );
237+ $ node2 ->expects ($ this ->exactly (2 ))
209238 ->method ('get ' )
210239 ->with ('su_event_localist_id ' )
211240 ->willReturn ($ field );
212- $ node ->expects ($ this ->once ())
213- ->method ('id ' )
214- ->willReturn (67890 );
215241
216- $ storage ->expects ($ this ->once ())
217- ->method ('loadMultiple ' )
218- ->willReturn ([67890 => $ node ]);
242+ $ storage = $ this ->createMock (EntityStorageInterface::class);
243+ $ storage ->expects ($ this ->exactly (2 ))
244+ ->method ('load ' )
245+ ->willReturnOnConsecutiveCalls ($ node1 , $ node2 );
219246
220- $ this ->entityTypeManager ->expects ($ this ->once ( ))
247+ $ this ->entityTypeManager ->expects ($ this ->exactly ( 2 ))
221248 ->method ('getStorage ' )
222249 ->with ('node ' )
223250 ->willReturn ($ storage );
0 commit comments