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