class LogTest

Same name in this branch
  1. 9 core/modules/migrate/tests/src/Unit/process/LogTest.php \Drupal\Tests\migrate\Unit\process\LogTest
  2. 9 core/tests/Drupal/Tests/Core/Database/LogTest.php \Drupal\Tests\Core\Database\LogTest
Same name and namespace in other branches
  1. 11.x core/modules/migrate/tests/src/Unit/process/LogTest.php \Drupal\Tests\migrate\Unit\process\LogTest
  2. 11.x core/modules/migrate/tests/src/Kernel/Plugin/LogTest.php \Drupal\Tests\migrate\Kernel\Plugin\LogTest

Tests the Log process plugin.

@group migrate

Hierarchy

Expanded class hierarchy of LogTest

File

core/modules/migrate/tests/src/Kernel/Plugin/LogTest.php, line 16

Namespace

Drupal\Tests\migrate\Kernel\Plugin
View source
class LogTest extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'node',
    'migrate',
  ];
  
  /**
   * The Log process plugin.
   *
   * @var \Drupal\migrate\Plugin\migrate\process\Log
   */
  protected $logPlugin;
  
  /**
   * Migrate executable.
   *
   * @var \Drupal\Tests\migrate\Kernel\Plugin\TestMigrateExecutable
   */
  protected $executable;
  
  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    parent::setUp();
    $definition = [
      'source' => [
        'plugin' => 'embedded_data',
        'data_rows' => [
          [
            'id' => '1',
          ],
        ],
        'ids' => [
          'id' => [
            'type' => 'integer',
          ],
        ],
      ],
      'destination' => [
        'plugin' => 'null',
      ],
    ];
    /** @var \Drupal\migrate\Plugin\migration $migration */
    $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
    $this->executable = new TestMigrateExecutable($migration);
    // Plugin being tested.
    $this->logPlugin = \Drupal::service('plugin.manager.migrate.process')->createInstance('log');
  }
  
  /**
   * Tests the Log plugin.
   */
  public function testLog() {
    $values = [
      'nid' => 2,
      'type' => 'page',
      'title' => 'page',
    ];
    $node = new Node($values, 'node', 'test');
    $node_array = <<<NODE
    Array
    (
        [nid] => Array
            (
            )
    
        [uuid] => Array
            (
            )
    
        [vid] => Array
            (
            )
    
        [langcode] => Array
            (
            )
    
        [type] => Array
            (
            )
    
        [revision_timestamp] => Array
            (
            )
    
        [revision_uid] => Array
            (
            )
    
        [revision_log] => Array
            (
            )
    
        [status] => Array
            (
            )
    
        [uid] => Array
            (
            )
    
        [title] => Array
            (
            )
    
        [created] => Array
            (
            )
    
        [changed] => Array
            (
            )
    
        [promote] => Array
            (
            )
    
        [sticky] => Array
            (
            )
    
        [default_langcode] => Array
            (
            )
    
        [revision_default] => Array
            (
            )
    
        [revision_translation_affected] => Array
            (
            )
    
    )
    
    NODE;
    $data = [
      'node' => [
        'value' => $node,
        'expected_message' => "'foo' value is Drupal\\node\\Entity\\Node:\n'{$node_array}'",
      ],
      'url' => [
        'value' => Url::fromUri('https://en.wikipedia.org/wiki/Drupal#Community'),
        'expected_message' => "'foo' value is Drupal\\Core\\Url:\n'https://en.wikipedia.org/wiki/Drupal#Community'",
      ],
    ];
    $i = 1;
    foreach ($data as $datum) {
      $this->executable->sourceIdValues = [
        'id' => $i++,
      ];
      // Test the input value is not altered.
      $new_value = $this->logPlugin
        ->transform($datum['value'], $this->executable, new Row(), 'foo');
      $this->assertSame($datum['value'], $new_value);
      // Test the stored message.
      $message = $this->executable
        ->getIdMap()
        ->getMessages($this->executable->sourceIdValues)
        ->fetchAllAssoc('message');
      $actual_message = key($message);
      $this->assertSame($datum['expected_message'], $actual_message);
    }
  }

}

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