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