3 namespace Drupal\Tests\migrate\Functional\process;
5 use Drupal\migrate\MigrateExecutable;
6 use Drupal\migrate\MigrateMessage;
7 use Drupal\migrate\Plugin\MigrateIdMapInterface;
8 use Drupal\migrate\Plugin\MigrationInterface;
9 use Drupal\Tests\BrowserTestBase;
12 * Tests the 'download' process plugin.
16 class DownloadFunctionalTest extends BrowserTestBase {
21 public static $modules = ['migrate', 'file'];
24 * Tests that an exception is thrown bu migration continues with the next row.
26 public function testExceptionThrow() {
27 $invalid_url = "{$this->baseUrl}/not-existent-404";
28 $valid_url = "{$this->baseUrl}/core/misc/favicon.ico";
32 'plugin' => 'embedded_data',
34 ['url' => $invalid_url, 'uri' => 'public://first.txt'],
35 ['url' => $valid_url, 'uri' => 'public://second.ico'],
38 'url' => ['type' => 'string'],
43 'plugin' => 'download',
44 'source' => ['url', 'uri'],
48 'plugin' => 'entity:file',
52 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
54 $executable = new MigrateExecutable($migration, new MigrateMessage());
55 $result = $executable->import();
57 // Check that the migration has completed.
58 $this->assertEquals($result, MigrationInterface::RESULT_COMPLETED);
60 /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map_plugin */
61 $id_map_plugin = $migration->getIdMap();
63 // Check that the first row was marked as failed in the id map table.
64 $map_row = $id_map_plugin->getRowBySource(['url' => $invalid_url]);
65 $this->assertEquals(MigrateIdMapInterface::STATUS_FAILED, $map_row['source_row_status']);
66 $this->assertNull($map_row['destid1']);
68 // Check that a message with the thrown exception has been logged.
69 $messages = $id_map_plugin->getMessageIterator(['url' => $invalid_url])->fetchAll();
70 $this->assertCount(1, $messages);
71 $message = reset($messages);
72 $this->assertEquals("Cannot read from non-readable stream ($invalid_url)", $message->message);
73 $this->assertEquals(MigrationInterface::MESSAGE_ERROR, $message->level);
75 // Check that the second row was migrated successfully.
76 $map_row = $id_map_plugin->getRowBySource(['url' => $valid_url]);
77 $this->assertEquals(MigrateIdMapInterface::STATUS_IMPORTED, $map_row['source_row_status']);
78 $this->assertEquals(1, $map_row['destid1']);