Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / migrate_drupal / tests / src / Unit / source / d6 / Drupal6SqlBaseTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\migrate_drupal\Unit\source\d6\Drupal6SqlBaseTest.
6  */
7
8 namespace Drupal\Tests\migrate_drupal\Unit\source\d6;
9
10 use Drupal\Tests\migrate\Unit\MigrateTestCase;
11
12 /**
13  * Tests the D6 SQL base class.
14  *
15  * @group migrate_drupal
16  */
17 class Drupal6SqlBaseTest extends MigrateTestCase {
18
19   /**
20    * Define bare minimum migration configuration.
21    */
22   protected $migrationConfiguration = [
23     'id' => 'Drupal6SqlBase',
24   ];
25
26   /**
27    * @var \Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase
28    */
29   protected $base;
30
31   /**
32    * Minimum database contents needed to test Drupal6SqlBase.
33    */
34   protected $databaseContents = [
35     'system' => [
36       [
37         'filename' => 'sites/all/modules/module1',
38         'name' => 'module1',
39         'type' => 'module',
40         'status' => 1,
41         'schema_version' => -1,
42       ],
43       [
44         'filename' => 'sites/all/modules/module2',
45         'name' => 'module2',
46         'type' => 'module',
47         'status' => 0,
48         'schema_version' => 7201,
49       ],
50       [
51         'filename' => 'sites/all/modules/test2',
52         'name' => 'test2',
53         'type' => 'theme',
54         'status' => 1,
55         'schema_version' => -1,
56       ],
57     ],
58     'variable' => [
59       [
60         'name' => 'my_variable',
61         'value' => 'b:1;',
62       ],
63     ],
64   ];
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function setUp() {
70     $plugin = 'placeholder_id';
71     /** @var \Drupal\Core\State\StateInterface $state */
72     $state = $this->getMock('Drupal\Core\State\StateInterface');
73     /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
74     $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
75     $this->base = new TestDrupal6SqlBase($this->migrationConfiguration, $plugin, [], $this->getMigration(), $state, $entity_manager);
76     $this->base->setDatabase($this->getDatabase($this->databaseContents));
77   }
78
79   /**
80    * Tests for Drupal6SqlBase::getSystemData().
81    */
82   public function testGetSystemData() {
83     $system_data = $this->base->getSystemData();
84     // Should be 1 theme and 2 modules.
85     $this->assertEquals(1, count($system_data['theme']));
86     $this->assertEquals(2, count($system_data['module']));
87
88     // Calling again should be identical.
89     $this->assertSame($system_data, $this->base->getSystemData());
90   }
91
92   /**
93    * Tests for Drupal6SqlBase::moduleExists().
94    */
95   public function testDrupal6ModuleExists() {
96     // This module should exist.
97     $this->assertTrue($this->base->moduleExistsWrapper('module1'));
98
99     // These modules should not exist.
100     $this->assertFalse($this->base->moduleExistsWrapper('module2'));
101     $this->assertFalse($this->base->moduleExistsWrapper('module3'));
102   }
103
104   /**
105    * Tests for Drupal6SqlBase::getModuleSchemaVersion().
106    */
107   public function testGetModuleSchemaVersion() {
108     // Non-existent module.
109     $this->assertFalse($this->base->getModuleSchemaVersionWrapper('module3'));
110
111     // Disabled module should still return schema version.
112     $this->assertEquals(7201, $this->base->getModuleSchemaVersionWrapper('module2'));
113
114     // Enabled module.
115     $this->assertEquals(-1, $this->base->getModuleSchemaVersionWrapper('module1'));
116   }
117
118   /**
119    * Tests for Drupal6SqlBase::variableGet().
120    */
121   public function testVariableGet() {
122     // Test default value.
123     $this->assertEquals('my_default', $this->base->variableGetWrapper('non_existent_variable', 'my_default'));
124
125     // Test non-default.
126     $this->assertSame(TRUE, $this->base->variableGetWrapper('my_variable', FALSE));
127   }
128
129 }
130
131 namespace Drupal\Tests\migrate_drupal\Unit\source\d6;
132
133 use Drupal\Core\Database\Connection;
134 use Drupal\Core\Extension\ModuleHandlerInterface;
135 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
136
137 /**
138  * Extends the Drupal6SqlBase abstract class.
139  */
140 class TestDrupal6SqlBase extends DrupalSqlBase {
141
142   /**
143    * {@inheritdoc}
144    */
145   public function fields() {
146     return [
147       'filename' => t('The path of the primary file for this item.'),
148       'name' => t('The name of the item; e.g. node.'),
149       'type' => t('The type of the item, either module, theme, or theme_engine.'),
150       'owner' => t("A theme's 'parent'. Can be either a theme or an engine."),
151       'status' => t('Boolean indicating whether or not this item is enabled.'),
152       'throttle' => t('Boolean indicating whether this item is disabled when the throttle.module disables throttleable items.'),
153       'bootstrap' => t("Boolean indicating whether this module is loaded during Drupal's early bootstrapping phase (e.g. even before the page cache is consulted)."),
154       'schema_version' => t("The module's database schema version number."),
155       'weight' => t("The order in which this module's hooks should be invoked."),
156       'info' => t("A serialized array containing information from the module's .info file."),
157     ];
158   }
159
160   /**
161    * {@inheritdoc}
162    */
163   public function query() {
164     $query = $this->database
165       ->select('system', 's')
166       ->fields('s', ['filename', 'name', 'schema_version']);
167     return $query;
168   }
169
170   /**
171    * Tweaks Drupal6SqlBase to set a new database connection for tests.
172    *
173    * @param \Drupal\Core\Database\Connection $database
174    *   The new connection to use.
175    *
176    * @see \Drupal\Tests\migrate\Unit\MigrateSqlTestCase
177    */
178   public function setDatabase(Connection $database) {
179     $this->database = $database;
180   }
181
182   /**
183    * Tweaks Drupal6SqlBase to set a new module handler for tests.
184    *
185    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
186    *   The new module handler to use.
187    *
188    * @see \Drupal\Tests\migrate\Unit\MigrateSqlTestCase
189    */
190   public function setModuleHandler(ModuleHandlerInterface $module_handler) {
191     $this->moduleHandler = $module_handler;
192   }
193
194   /**
195    * Wrapper method to test protected method moduleExists().
196    */
197   public function moduleExistsWrapper($module) {
198     return parent::moduleExists($module);
199   }
200
201   /**
202    * Wrapper method to test protected method getModuleSchemaVersion().
203    */
204   public function getModuleSchemaVersionWrapper($module) {
205     return parent::getModuleSchemaVersion($module);
206   }
207
208   /**
209    * Wrapper method to test protected method variableGet().
210    */
211   public function variableGetWrapper($name, $default) {
212     return parent::variableGet($name, $default);
213   }
214
215   /**
216    * {@inheritdoc}
217    */
218   public function getIds() {
219     return [];
220   }
221
222 }