You are here

public function MigrateSqlIdMapTest::testMessageSave in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php \Drupal\Tests\migrate\Unit\MigrateSqlIdMapTest::testMessageSave()

Tests the SQL ID map save message method.

File

core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php, line 295

Class

MigrateSqlIdMapTest
Tests the SQL ID map plugin.

Namespace

Drupal\Tests\migrate\Unit

Code

public function testMessageSave() {
  $message = 'Hello world.';
  $original_values = [
    1 => [
      'message' => $message,
      'level' => MigrationInterface::MESSAGE_ERROR,
    ],
    2 => [
      'message' => $message,
      'level' => MigrationInterface::MESSAGE_WARNING,
    ],
    3 => [
      'message' => $message,
      'level' => MigrationInterface::MESSAGE_NOTICE,
    ],
    4 => [
      'message' => $message,
      'level' => MigrationInterface::MESSAGE_INFORMATIONAL,
    ],
  ];
  $expected_results = [
    '7ad742edb7e866caa78ced1e4455d2e9cbd8adb2074e7c323d21b4e67732e755' => [
      'message' => $message,
      'level' => MigrationInterface::MESSAGE_ERROR,
    ],
    '2d3ec2b0c547e819346e6ae03f881fd9f5c978ff3cbe29dfb807d40735e53703' => [
      'message' => $message,
      'level' => MigrationInterface::MESSAGE_WARNING,
    ],
    '12a042f72cad9a2a8c7715df0c7695d762975f0687d87f5d480725dae1432a6f' => [
      'message' => $message,
      'level' => MigrationInterface::MESSAGE_NOTICE,
    ],
    'd9d1fd27a2447ace48f47a2e9ff649673f67b446d9381a7963c949fc083f8791' => [
      'message' => $message,
      'level' => MigrationInterface::MESSAGE_INFORMATIONAL,
    ],
  ];
  $id_map = $this
    ->getIdMap();
  foreach ($original_values as $key => $original_value) {
    $id_map
      ->saveMessage([
      'source_id_property' => $key,
    ], $message, $original_value['level']);
  }
  foreach ($id_map
    ->getMessages() as $message_row) {
    $key = $message_row->source_ids_hash;
    $this
      ->assertEquals($expected_results[$key]['message'], $message_row->message);
    $this
      ->assertEquals($expected_results[$key]['level'], $message_row->level);
  }

  // Insert with default level.
  $message_default = 'Hello world default.';
  $id_map
    ->saveMessage([
    'source_id_property' => 5,
  ], $message_default);
  $messages = $id_map
    ->getMessages([
    'source_id_property' => 5,
  ]);
  $count = 0;
  foreach ($messages as $key => $message_row) {
    $count = 1;
    $this
      ->assertEquals($message_default, $message_row->message);
    $this
      ->assertEquals(MigrationInterface::MESSAGE_ERROR, $message_row->level);
  }
  $this
    ->assertEquals(1, $count);

  // Retrieve messages with a specific level.
  $messages = $id_map
    ->getMessages([], MigrationInterface::MESSAGE_WARNING);
  $count = 0;
  foreach ($messages as $key => $message_row) {
    $count = 1;
    $this
      ->assertEquals(MigrationInterface::MESSAGE_WARNING, $message_row->level);
  }
  $this
    ->assertEquals(1, $count);
}