function DownloadFunctionalTest::testExceptionThrow

Same name and namespace in other branches
  1. 9 core/modules/migrate/tests/src/Functional/process/DownloadFunctionalTest.php \Drupal\Tests\migrate\Functional\process\DownloadFunctionalTest::testExceptionThrow()
  2. 8.9.x core/modules/migrate/tests/src/Functional/process/DownloadFunctionalTest.php \Drupal\Tests\migrate\Functional\process\DownloadFunctionalTest::testExceptionThrow()
  3. 11.x core/modules/migrate/tests/src/Functional/process/DownloadFunctionalTest.php \Drupal\Tests\migrate\Functional\process\DownloadFunctionalTest::testExceptionThrow()

Tests that an exception is thrown bu migration continues with the next row.

File

core/modules/migrate/tests/src/Functional/process/DownloadFunctionalTest.php, line 34

Class

DownloadFunctionalTest
Tests the 'download' process plugin.

Namespace

Drupal\Tests\migrate\Functional\process

Code

public function testExceptionThrow() : void {
  $invalid_url = "{$this->baseUrl}/not-existent-404";
  $valid_url = "{$this->baseUrl}/core/misc/favicon.ico";
  $definition = [
    'source' => [
      'plugin' => 'embedded_data',
      'data_rows' => [
        [
          'url' => $invalid_url,
          'uri' => 'public://first.txt',
        ],
        [
          'url' => $valid_url,
          'uri' => 'public://second.ico',
        ],
      ],
      'ids' => [
        'url' => [
          'type' => 'string',
        ],
      ],
    ],
    'process' => [
      'uri' => [
        'plugin' => 'download',
        'source' => [
          'url',
          'uri',
        ],
      ],
    ],
    'destination' => [
      'plugin' => 'entity:file',
    ],
  ];
  $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
  $executable = new MigrateExecutable($migration);
  $result = $executable->import();
  // Check that the migration has completed.
  $this->assertEquals(MigrationInterface::RESULT_COMPLETED, $result);
  /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map_plugin */
  $id_map_plugin = $migration->getIdMap();
  // Check that the first row was marked as failed in the id map table.
  $map_row = $id_map_plugin->getRowBySource([
    'url' => $invalid_url,
  ]);
  $this->assertEquals(MigrateIdMapInterface::STATUS_FAILED, $map_row['source_row_status']);
  $this->assertNull($map_row['destid1']);
  // Check that a message with the thrown exception has been logged.
  $messages = $id_map_plugin->getMessages([
    'url' => $invalid_url,
  ])
    ->fetchAll();
  $this->assertCount(1, $messages);
  $message = reset($messages);
  // Assert critical parts of the error message, but not the exact message,
  // since it depends on Guzzle's internal implementation of PSR-7.
  $id = $migration->getPluginId();
  $this->assertStringContainsString("{$id}:uri:download:", $message->message);
  $this->assertStringContainsString($invalid_url, $message->message);
  $this->assertEquals(MigrationInterface::MESSAGE_ERROR, $message->level);
  // Check that the second row was migrated successfully.
  $map_row = $id_map_plugin->getRowBySource([
    'url' => $valid_url,
  ]);
  $this->assertEquals(MigrateIdMapInterface::STATUS_IMPORTED, $map_row['source_row_status']);
  $this->assertEquals(1, $map_row['destid1']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.