You are here

DrushCommandsTest.php in Migrate Tools 8.4

Same filename and directory in other branches
  1. 8.5 tests/src/Functional/DrushCommandsTest.php

File

tests/src/Functional/DrushCommandsTest.php
View source
<?php

namespace Drupal\Tests\migrate_tools\Functional;

use Drupal\Tests\BrowserTestBase;
use Drush\TestTraits\DrushTestTrait;

/**
 * Execute drush on fully functional website.
 *
 * @group migrate_tools
 */
class DrushCommandsTest extends BrowserTestBase {
  use DrushTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'migrate_tools_test',
    'migrate_tools',
    'migrate_plus',
    'taxonomy',
    'text',
    'system',
    'user',
  ];

  /**
   * Tests migrate:import with feedback.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function testFeedback() {
    $this
      ->drush('mim', [
      'fruit_terms',
    ], [
      'feedback' => 2,
    ]);
    $expected = [
      '1/3 [=========>------------------]  33%',
      ' 2/3 [==================>---------]  66% [notice] Processed 2 items (2 created, 0 updated, 0 failed, 0 ignored) - continuing with \'fruit_terms\'',
      '',
      ' 3/3 [============================] 100% [notice] Processed 1 item (1 created, 0 updated, 0 failed, 0 ignored) - done with \'fruit_terms\'',
    ];
    $this
      ->assertEquals($expected, $this
      ->getErrorOutputAsList());
  }

  /**
   * Tests migrate:import with limit.
   */
  public function testLimit() {
    $this
      ->drush('mim', [
      'fruit_terms',
    ], [
      'limit' => 2,
    ]);
    $expected = [
      '1/3 [=========>------------------]  33%',
      ' 2/3 [==================>---------]  66% [notice] Processed 2 items (2 created, 0 updated, 0 failed, 0 ignored) - done with \'fruit_terms\'',
    ];
    $this
      ->assertEquals($expected, $this
      ->getErrorOutputAsList());
  }

  /**
   * Test that migrations continue after a failure if the option is set.
   */
  public function testContinueOnFailure() {

    // Option not set, fruit_terms should not run.
    $this
      ->drush('mim', [
      'invalid_plugin,fruit_terms',
    ], [], NULL, NULL, 1);
    $this
      ->assertNotContains("done with 'fruit_terms'", $this
      ->getErrorOutput());

    // Option set, fruit_terms should run.
    $this
      ->drush('mim', [
      'invalid_plugin,fruit_terms',
    ], [
      'continue-on-failure' => NULL,
    ]);
    $this
      ->assertContains("done with 'fruit_terms'", $this
      ->getErrorOutput());

    // Option not set, fruit_terms should not run.
    $this
      ->drush('mr', [
      'invalid_plugin,fruit_terms',
    ], [], NULL, NULL, 1);
    $this
      ->assertNotContains("done with 'fruit_terms'", $this
      ->getErrorOutput());

    // Option set, fruit_terms should run.
    $this
      ->drush('mr', [
      'invalid_plugin,fruit_terms',
    ], [
      'continue-on-failure' => NULL,
    ]);
    $this
      ->assertContains("done with 'fruit_terms'", $this
      ->getErrorOutput());

    // Option not set, fruit_terms should not display.
    $this
      ->drush('ms', [
      'invalid_plugin,fruit_terms',
    ], [
      'format' => 'json',
    ], NULL, NULL, 1);

    // This demonstrates we surface the exception but not as an error.
    $this
      ->assertNotContains('[error]  The "does_not_exist" plugin does not exist', $this
      ->getErrorOutput());
    $this
      ->assertContains('The "does_not_exist" plugin does not exist', $this
      ->getErrorOutput());
    $this
      ->assertNotContains('fruit_terms    Idle     3', $this
      ->getOutput());

    // Option set, fruit_terms should display.
    $this
      ->drush('ms', [
      'invalid_plugin,fruit_terms',
    ], [
      'continue-on-failure' => NULL,
    ]);
    $this
      ->assertContains('[error]  The "does_not_exist" plugin does not exist', $this
      ->getErrorOutput());
    $this
      ->assertContains('fruit_terms    Idle     3', $this
      ->getOutput());
  }

  /**
   * Tests many of the migrate drush commands.
   */
  public function testDrush() {
    $this
      ->drush('ms', [], [], NULL, NULL, 1);
    $this
      ->assertContains('The "does_not_exist" plugin does not exist.', $this
      ->getErrorOutput());
    $this->container
      ->get('config.factory')
      ->getEditable('migrate_plus.migration.invalid_plugin')
      ->delete();

    // Flush cache so the recently removed invalid migration is cleared.
    drupal_flush_all_caches();
    $this
      ->drush('ms', [], [
      'format' => 'json',
    ]);
    $expected = [
      [
        'group' => 'Default (default)',
        'id' => 'fruit_terms',
        'imported' => 0,
        'status' => 'Idle',
        'total' => 3,
        'unprocessed' => 3,
        'last_imported' => '',
      ],
      [
        'group' => 'Default (default)',
        'id' => 'source_exception',
        'imported' => 0,
        'status' => 'Idle',
        'total' => 0,
        'unprocessed' => 0,
        'last_imported' => '',
      ],
    ];
    $this
      ->assertEquals($expected, $this
      ->getOutputFromJSON());
    $expected = [
      '1/3 [=========>------------------]  33%',
      ' 2/3 [==================>---------]  66%',
      ' 3/3 [============================] 100% [notice] Processed 3 items (3 created, 0 updated, 0 failed, 0 ignored) - done with \'fruit_terms\'',
    ];
    $this
      ->drush('mim', [
      'fruit_terms',
    ]);
    $this
      ->assertEquals($expected, $this
      ->getErrorOutputAsList());
    $expected = [
      '1/3 [=========>------------------]  33%',
      ' 2/3 [==================>---------]  66%',
      ' 3/3 [============================] 100% [notice] Processed 3 items (0 created, 3 updated, 0 failed, 0 ignored) - done with \'fruit_terms\'',
    ];
    $this
      ->drush('mim', [
      'fruit_terms',
    ], [
      'update' => NULL,
      'force' => NULL,
      'execute-dependencies' => NULL,
    ]);
    $this
      ->assertEquals($expected, $this
      ->getErrorOutputAsList());
    $this
      ->drush('mrs', [
      'fruit_terms',
    ]);
    $this
      ->assertErrorOutputEquals('[warning] Migration fruit_terms is already Idle');
    $this
      ->drush('mfs', [
      'fruit_terms',
    ], [
      'format' => 'json',
    ]);
    $expected = [
      [
        'machine_name' => 'name',
        'description' => 'name',
      ],
    ];
    $this
      ->assertEquals($expected, $this
      ->getOutputFromJSON());
    $this
      ->drush('mr', [
      'fruit_terms',
    ]);
    $expected = [
      '1/3 [=========>------------------]  33%',
      ' 2/3 [==================>---------]  66%',
      ' 3/3 [============================] 100% [notice] Rolled back 3 items - done with \'fruit_terms\'',
    ];
    $this
      ->assertEquals($expected, $this
      ->getErrorOutputAsList());
    $this
      ->drush('migrate:stop', [
      'fruit_terms',
    ]);
    $this
      ->assertErrorOutputEquals('[warning] Migration fruit_terms is idle');
    $this
      ->drush('mim', [
      'fruit_terms',
    ], [
      'skip-progress-bar' => NULL,
    ]);
    $this
      ->assertErrorOutputEquals('[notice] Processed 3 items (3 created, 0 updated, 0 failed, 0 ignored) - done with \'fruit_terms\'');
    $this
      ->drush('mr', [
      'fruit_terms',
    ], [
      'skip-progress-bar' => NULL,
    ]);
    $this
      ->assertErrorOutputEquals('[notice] Rolled back 3 items - done with \'fruit_terms\'');
  }

  /**
   * Fully test migrate messages.
   */
  public function testMessages() {
    $this
      ->drush('mim', [
      'fruit_terms',
    ]);
    $this
      ->drush('mmsg', [
      'fruit_terms',
    ]);
    $this
      ->assertErrorOutputEquals('[notice] No messages for this migration');

    /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map */
    $id_map = $this->container
      ->get('plugin.manager.migration')
      ->createInstance('fruit_terms')
      ->getIdMap();
    $id_map
      ->saveMessage([
      'name' => 'Apple',
    ], 'You picked a bad one.');
    $this
      ->drush('mmsg', [
      'fruit_terms',
    ], [
      'format' => 'json',
    ]);
    $expected = [
      [
        'level' => '1',
        'message' => 'You picked a bad one.',
        'source_ids' => 'Apple',
        'destination_ids' => '1',
      ],
    ];
    $this
      ->assertEquals($expected, $this
      ->getOutputFromJSON());
    $this
      ->drush('mmsg', [
      'fruit_terms',
    ], [
      'format' => 'csv',
    ]);
    $expected = <<<EOT
"Source ID(s)","Destination ID(s)",Level,Message
Apple,1,1,"You picked a bad one."
EOT;
    $this
      ->assertEquals($expected, $this
      ->getOutput());
  }

  /**
   * Tests synced import.
   */
  public function testSyncImport() {
    $this
      ->drush('mim', [
      'fruit_terms',
    ]);
    $expected = [
      '1/3 [=========>------------------]  33%',
      ' 2/3 [==================>---------]  66%',
      ' 3/3 [============================] 100% [notice] Processed 3 items (3 created, 0 updated, 0 failed, 0 ignored) - done with \'fruit_terms\'',
    ];
    $this
      ->assertEquals($expected, $this
      ->getErrorOutputAsList());
    $term = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->load(2);
    $this
      ->assertEquals('Banana', $term
      ->label());
    $this
      ->assertEquals(3, \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->getQuery()
      ->count()
      ->execute());
    $source = $this->container
      ->get('config.factory')
      ->getEditable('migrate_plus.migration.fruit_terms')
      ->get('source');
    unset($source['data_rows'][1]);
    $source['data_rows'][] = [
      'name' => 'Grape',
    ];
    $this->container
      ->get('config.factory')
      ->getEditable('migrate_plus.migration.fruit_terms')
      ->set('source', $source)
      ->save();

    // Flush cache so the recently changed migration can be refreshed.
    drupal_flush_all_caches();
    $this
      ->drush('mim', [
      'fruit_terms',
    ], [
      'sync' => NULL,
    ]);
    $expected = [
      '1/3 [=========>------------------]  33% [notice] Rolled back 1 item - done with \'fruit_terms\'',
      '',
      ' 2/3 [==================>---------]  66%',
      ' 3/3 [============================] 100%',
      ' 4/4 [============================] 100% [notice] Processed 3 items (1 created, 2 updated, 0 failed, 0 ignored) - done with \'fruit_terms\'',
    ];
    $this
      ->assertEquals($expected, $this
      ->getErrorOutputAsList());
    $this
      ->assertEquals(3, \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->getQuery()
      ->count()
      ->execute());
    $this
      ->assertEmpty(\Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->load(2));

    /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map */
    $id_map = $this->container
      ->get('plugin.manager.migration')
      ->createInstance('fruit_terms')
      ->getIdMap();
    $this
      ->assertCount(3, $id_map);
  }

}

Classes

Namesort descending Description
DrushCommandsTest Execute drush on fully functional website.