5e890a0e6a0756326b4376f1d4ff9cc0551c8d9a
[yaffs-website] / web / core / modules / migrate / tests / src / Functional / process / DownloadFunctionalTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Functional\process;
4
5 use Drupal\migrate\MigrateExecutable;
6 use Drupal\migrate\Plugin\MigrateIdMapInterface;
7 use Drupal\migrate\Plugin\MigrationInterface;
8 use Drupal\Tests\BrowserTestBase;
9
10 /**
11  * Tests the 'download' process plugin.
12  *
13  * @group migrate
14  */
15 class DownloadFunctionalTest extends BrowserTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['migrate', 'file'];
21
22   /**
23    * Tests that an exception is thrown bu migration continues with the next row.
24    */
25   public function testExceptionThrow() {
26     $invalid_url = "{$this->baseUrl}/not-existent-404";
27     $valid_url = "{$this->baseUrl}/core/misc/favicon.ico";
28
29     $definition = [
30       'source' => [
31         'plugin' => 'embedded_data',
32         'data_rows' => [
33           ['url' => $invalid_url, 'uri' => 'public://first.txt'],
34           ['url' => $valid_url, 'uri' => 'public://second.ico'],
35         ],
36         'ids' => [
37           'url' => ['type' => 'string'],
38         ],
39       ],
40       'process' => [
41         'uri' => [
42           'plugin' => 'download',
43           'source' => ['url', 'uri'],
44         ],
45       ],
46       'destination' => [
47         'plugin' => 'entity:file',
48       ],
49     ];
50
51     $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
52
53     $executable = new MigrateExecutable($migration);
54     $result = $executable->import();
55
56     // Check that the migration has completed.
57     $this->assertEquals($result, MigrationInterface::RESULT_COMPLETED);
58
59     /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map_plugin */
60     $id_map_plugin = $migration->getIdMap();
61
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']);
66
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);
73
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']);
78   }
79
80 }