Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / MigrationPluginManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit;
4
5 use Drupal\migrate\Plugin\Migration;
6 use Drupal\migrate\Plugin\MigrationPluginManager;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * @coversDefaultClass \Drupal\migrate\Plugin\MigrationPluginManager
11  * @group migrate
12  */
13 class MigrationPluginManagerTest extends UnitTestCase {
14
15   /**
16    * A plugin manager.
17    *
18    * @var \Drupal\migrate\Plugin\MigrationPluginManager
19    */
20   protected $pluginManager;
21
22   /**
23    * {@inheritdoc}
24    */
25   public function setUp() {
26     parent::setUp();
27
28     // Get a plugin manager for testing.
29     $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
30     $cache_backend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
31     $language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
32     $this->pluginManager = new MigrationPluginManager($module_handler, $cache_backend, $language_manager);
33   }
34
35   /**
36    * Tests building dependencies for multiple migrations.
37    *
38    * @dataProvider dependencyProvider
39    */
40   public function testDependencyBuilding($migrations_data, $result_ids) {
41     $migrations = [];
42     foreach ($migrations_data as $migration_id => $migration_data) {
43       $migrations[$migration_id] = new TestMigrationMock($migration_id, $migration_data['dependencies']);
44     }
45
46     $ordered_migrations = $this->pluginManager->buildDependencyMigration($migrations, []);
47
48     // Verify results.
49     $this->assertEquals($result_ids, array_keys($ordered_migrations));
50     foreach ($migrations_data as $migration_id => $migration_data) {
51       $migration = $migrations[$migration_id];
52
53       $requirements = $migration_data['result_requirements'];
54       if (empty($requirements)) {
55         $this->assertEquals([], $migration->set);
56       }
57       else {
58         $requirements = array_combine($requirements, $requirements);
59
60         $this->assertEquals(1, count($migration->set));
61         list($set_prop, $set_requirements) = reset($migration->set);
62         $this->assertEquals('requirements', $set_prop);
63         $this->assertEquals($requirements, $set_requirements);
64       }
65     }
66   }
67
68   /**
69    * Provide dependency data for testing.
70    */
71   public function dependencyProvider() {
72     return [
73       // Just one migration, with no dependencies.
74       [
75         [
76           'm1' => [
77             'dependencies' => [],
78             'result_requirements' => [],
79           ],
80         ],
81         ['m1'],
82       ],
83
84       // Just one migration, with required dependencies.
85       [
86         [
87           'm1' => [
88             'dependencies' => [
89               'required' => ['required1', 'required2'],
90             ],
91             'result_requirements' => ['required1', 'required2'],
92           ],
93         ],
94         ['m1'],
95       ],
96
97       // Just one migration, with optional dependencies.
98       [
99         [
100           'm1' => [
101             'dependencies' => [
102               'optional' => ['optional1'],
103             ],
104             'result_requirements' => [],
105           ],
106         ],
107         ['m1'],
108       ],
109
110       // Multiple migrations.
111       [
112         [
113           'm1' => [
114             'dependencies' => [
115               'required' => ['required1', 'required2'],
116             ],
117             'result_requirements' => ['required1', 'required2'],
118           ],
119           'm2' => [
120             'dependencies' => [
121               'optional' => ['optional1'],
122             ],
123             'result_requirements' => [],
124           ],
125         ],
126         ['m1', 'm2'],
127       ],
128
129       // Multiple migrations, reordered due to optional requirement.
130       [
131         [
132           'm1' => [
133             'dependencies' => [
134               'optional' => ['m2'],
135             ],
136             'result_requirements' => [],
137           ],
138           'm2' => [
139             'dependencies' => [
140               'optional' => ['optional1'],
141             ],
142             'result_requirements' => [],
143           ],
144         ],
145         ['m2', 'm1'],
146       ],
147
148       // Ensure that optional requirements aren't turned into required ones,
149       // if the last migration has no optional deps.
150       [
151         [
152           'm1' => [
153             'dependencies' => [
154               'optional' => ['m2'],
155             ],
156             'result_requirements' => [],
157           ],
158           'm2' => [
159             'dependencies' => [],
160             'result_requirements' => [],
161           ],
162         ],
163         ['m2', 'm1'],
164       ],
165     ];
166   }
167
168 }
169
170 /**
171  * A mock migration plugin.
172  *
173  * Why are we using a custom class here?
174  *
175  * 1. The function buildDependencyMigration() calls $migration->set(), which
176  * is not actually in MigrationInterface.
177  *
178  * 2. The function buildDependencyMigration() calls array_multisort on an
179  * array with mocks in it. PHPUnit mocks are really complex, and if PHP tries
180  * to compare them it will die with "Nesting level too deep".
181  */
182 class TestMigrationMock extends Migration {
183   /**
184    * The values passed into set().
185    *
186    * @var array
187    */
188   public $set = [];
189
190   /**
191    * TestMigrationMock constructor.
192    */
193   public function __construct($id, $dependencies) {
194     // Intentionally ignore parent constructor.
195     $this->id = $id;
196     $this->dependencies = $dependencies;
197   }
198
199   /**
200    * {@inheritdoc}
201    */
202   public function id() {
203     return $this->id;
204   }
205
206   /**
207    * {@inheritdoc}
208    */
209   public function getMigrationDependencies() {
210     return $this->dependencies;
211   }
212
213   /**
214    * {@inheritdoc}
215    */
216   public function set($prop, $value) {
217     $this->set[] = func_get_args();
218   }
219
220 }