DrupalSqlBaseTest.php 3.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

namespace Drupal\Tests\migrate_drupal\Unit\source;

use Drupal\Tests\migrate\Unit\MigrateTestCase;
use Drupal\migrate\Exception\RequirementsException;

/**
 * @coversDefaultClass Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase
 * @group migrate_drupal
 */
class DrupalSqlBaseTest extends MigrateTestCase {

  /**
   * Define bare minimum migration configuration.
   */
17
  protected $migrationConfiguration = [
18
    'id' => 'DrupalSqlBase',
19
  ];
20 21 22 23 24 25 26 27 28

  /**
   * @var \Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase
  */
  protected $base;

  /**
   * Minimum database contents needed to test DrupalSqlBase.
   */
29 30 31
  protected $databaseContents = [
    'system' => [
      [
32 33 34 35 36
        'filename' => 'sites/all/modules/module1',
        'name' => 'module1',
        'type' => 'module',
        'status' => 0,
        'schema_version' => -1,
37 38 39
      ],
    ],
  ];
40 41 42 43 44 45

  /**
   * @covers ::checkRequirements
   */
  public function testSourceProviderNotActive() {
    $plugin_definition['requirements_met'] = TRUE;
46
    $plugin_definition['source_module'] = 'module1';
47 48 49 50 51 52 53
    /** @var \Drupal\Core\State\StateInterface $state */
    $state = $this->getMock('Drupal\Core\State\StateInterface');
    /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
    $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
    $plugin = new TestDrupalSqlBase([], 'placeholder_id', $plugin_definition, $this->getMigration(), $state, $entity_manager);
    $plugin->setDatabase($this->getDatabase($this->databaseContents));
    $system_data = $plugin->getSystemData();
54 55 56 57 58 59
    $this->setExpectedException(RequirementsException::class, 'The module module1 is not enabled in the source site.');
    try {
      $plugin->checkRequirements();
    }
    catch (RequirementsException $e) {
      // Ensure requirements are set on the exception.
60
      $this->assertEquals(['source_module' => 'module1'], $e->getRequirements());
61 62 63
      // Re-throw so PHPUnit can assert the exception.
      throw $e;
    }
64 65
  }

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  /**
   * @covers ::checkRequirements
   */
  public function testSourceDatabaseError() {
    $plugin_definition['requirements_met'] = TRUE;
    $plugin_definition['source_module'] = 'module1';
    /** @var \Drupal\Core\State\StateInterface $state */
    $state = $this->getMock('Drupal\Core\State\StateInterface');
    /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
    $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
    $plugin = new TestDrupalSqlBase([], 'test', $plugin_definition, $this->getMigration(), $state, $entity_manager);
    $system_data = $plugin->getSystemData();
    $this->setExpectedException(RequirementsException::class, 'No database connection configured for source plugin test');
    $plugin->checkRequirements();
  }

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
}

namespace Drupal\Tests\migrate_drupal\Unit\source;

use Drupal\Core\Database\Connection;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;

/**
 * Extends the DrupalSqlBase abstract class.
 */
class TestDrupalSqlBase extends DrupalSqlBase {

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
  }

  /**
   * Tweaks DrupalSqlBase to set a new database connection for tests.
   *
110
   * @param \Drupal\Core\Database\Connection $database
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
   *   The new connection to use.
   *
   * @see \Drupal\Tests\migrate\Unit\MigrateSourceSqlTestCase
   */
  public function setDatabase(Connection $database) {
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    return [];
  }

}