Pull merge.
[yaffs-website] / web / core / modules / migrate_drupal / tests / src / Unit / source / DrupalSqlBaseTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate_drupal\Unit\source;
4
5 use Drupal\Tests\migrate\Unit\MigrateTestCase;
6 use Drupal\migrate\Exception\RequirementsException;
7
8 /**
9  * @coversDefaultClass Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase
10  * @group migrate_drupal
11  */
12 class DrupalSqlBaseTest extends MigrateTestCase {
13
14   /**
15    * Define bare minimum migration configuration.
16    */
17   protected $migrationConfiguration = [
18     'id' => 'DrupalSqlBase',
19   ];
20
21   /**
22    * @var \Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase
23   */
24   protected $base;
25
26   /**
27    * Minimum database contents needed to test DrupalSqlBase.
28    */
29   protected $databaseContents = [
30     'system' => [
31       [
32         'filename' => 'sites/all/modules/module1',
33         'name' => 'module1',
34         'type' => 'module',
35         'status' => 0,
36         'schema_version' => -1,
37       ],
38     ],
39   ];
40
41   /**
42    * @covers ::checkRequirements
43    */
44   public function testSourceProviderNotActive() {
45     $plugin_definition['requirements_met'] = TRUE;
46     $plugin_definition['source_module'] = 'module1';
47     /** @var \Drupal\Core\State\StateInterface $state */
48     $state = $this->getMock('Drupal\Core\State\StateInterface');
49     /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
50     $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
51     $plugin = new TestDrupalSqlBase([], 'placeholder_id', $plugin_definition, $this->getMigration(), $state, $entity_manager);
52     $plugin->setDatabase($this->getDatabase($this->databaseContents));
53     $system_data = $plugin->getSystemData();
54     $this->setExpectedException(RequirementsException::class, 'The module module1 is not enabled in the source site.');
55     try {
56       $plugin->checkRequirements();
57     }
58     catch (RequirementsException $e) {
59       // Ensure requirements are set on the exception.
60       $this->assertEquals(['source_module' => 'module1'], $e->getRequirements());
61       // Re-throw so PHPUnit can assert the exception.
62       throw $e;
63     }
64   }
65
66   /**
67    * @covers ::checkRequirements
68    */
69   public function testSourceDatabaseError() {
70     $plugin_definition['requirements_met'] = TRUE;
71     $plugin_definition['source_module'] = 'module1';
72     /** @var \Drupal\Core\State\StateInterface $state */
73     $state = $this->getMock('Drupal\Core\State\StateInterface');
74     /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
75     $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
76     $plugin = new TestDrupalSqlBase([], 'test', $plugin_definition, $this->getMigration(), $state, $entity_manager);
77     $system_data = $plugin->getSystemData();
78     $this->setExpectedException(RequirementsException::class, 'No database connection configured for source plugin test');
79     $plugin->checkRequirements();
80   }
81
82 }
83
84 namespace Drupal\Tests\migrate_drupal\Unit\source;
85
86 use Drupal\Core\Database\Connection;
87 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
88
89 /**
90  * Extends the DrupalSqlBase abstract class.
91  */
92 class TestDrupalSqlBase extends DrupalSqlBase {
93
94   /**
95    * {@inheritdoc}
96    */
97   public function fields() {
98     return [];
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function query() {
105   }
106
107   /**
108    * Tweaks DrupalSqlBase to set a new database connection for tests.
109    *
110    * @param \Drupal\Core\Database\Connection $database
111    *   The new connection to use.
112    *
113    * @see \Drupal\Tests\migrate\Unit\MigrateSourceSqlTestCase
114    */
115   public function setDatabase(Connection $database) {
116     $this->database = $database;
117   }
118
119   /**
120    * {@inheritdoc}
121    */
122   public function getIds() {
123     return [];
124   }
125
126 }