PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php

https://gitlab.com/guillaumev/alkarama
PHP | 449 lines | 294 code | 90 blank | 65 comment | 0 complexity | d6a9b706c67bacf1ea578dacbc374d7b MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\migrate\Unit;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\migrate\Plugin\MigrationInterface;
  5. use Drupal\migrate\Plugin\MigrateIdMapInterface;
  6. use Drupal\migrate\MigrateException;
  7. use Drupal\migrate\Row;
  8. /**
  9. * @coversDefaultClass \Drupal\migrate\MigrateExecutable
  10. * @group migrate
  11. */
  12. class MigrateExecutableTest extends MigrateTestCase {
  13. /**
  14. * The mocked migration entity.
  15. *
  16. * @var \Drupal\migrate\Plugin\MigrationInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $migration;
  19. /**
  20. * The mocked migrate message.
  21. *
  22. * @var \Drupal\migrate\MigrateMessageInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $message;
  25. /**
  26. * The tested migrate executable.
  27. *
  28. * @var \Drupal\Tests\migrate\Unit\TestMigrateExecutable
  29. */
  30. protected $executable;
  31. /**
  32. * The migration's configuration values.
  33. *
  34. * @var array
  35. */
  36. protected $migrationConfiguration = array(
  37. 'id' => 'test',
  38. );
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function setUp() {
  43. parent::setUp();
  44. $this->migration = $this->getMigration();
  45. $this->message = $this->getMock('Drupal\migrate\MigrateMessageInterface');
  46. $event_dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  47. $this->executable = new TestMigrateExecutable($this->migration, $this->message, $event_dispatcher);
  48. $this->executable->setStringTranslation($this->getStringTranslationStub());
  49. }
  50. /**
  51. * Tests an import with an incomplete rewinding.
  52. */
  53. public function testImportWithFailingRewind() {
  54. $exception_message = $this->getRandomGenerator()->string();
  55. $source = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
  56. $source->expects($this->once())
  57. ->method('rewind')
  58. ->will($this->throwException(new \Exception($exception_message)));
  59. $this->migration->expects($this->any())
  60. ->method('getSourcePlugin')
  61. ->will($this->returnValue($source));
  62. // Ensure that a message with the proper message was added.
  63. $this->message->expects($this->once())
  64. ->method('display')
  65. ->with("Migration failed with source plugin exception: " . Html::escape($exception_message));
  66. $result = $this->executable->import();
  67. $this->assertEquals(MigrationInterface::RESULT_FAILED, $result);
  68. }
  69. /**
  70. * Tests the import method with a valid row.
  71. */
  72. public function testImportWithValidRow() {
  73. $source = $this->getMockSource();
  74. $row = $this->getMockBuilder('Drupal\migrate\Row')
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $row->expects($this->once())
  78. ->method('getSourceIdValues')
  79. ->will($this->returnValue(array('id' => 'test')));
  80. $this->idMap->expects($this->once())
  81. ->method('lookupDestinationId')
  82. ->with(array('id' => 'test'))
  83. ->will($this->returnValue(array('test')));
  84. $source->expects($this->once())
  85. ->method('current')
  86. ->will($this->returnValue($row));
  87. $this->executable->setSource($source);
  88. $this->migration->expects($this->once())
  89. ->method('getProcessPlugins')
  90. ->will($this->returnValue(array()));
  91. $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
  92. $destination->expects($this->once())
  93. ->method('import')
  94. ->with($row, array('test'))
  95. ->will($this->returnValue(array('id' => 'test')));
  96. $this->migration
  97. ->method('getDestinationPlugin')
  98. ->willReturn($destination);
  99. $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
  100. }
  101. /**
  102. * Tests the import method with a valid row.
  103. */
  104. public function testImportWithValidRowWithoutDestinationId() {
  105. $source = $this->getMockSource();
  106. $row = $this->getMockBuilder('Drupal\migrate\Row')
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $row->expects($this->once())
  110. ->method('getSourceIdValues')
  111. ->will($this->returnValue(array('id' => 'test')));
  112. $this->idMap->expects($this->once())
  113. ->method('lookupDestinationId')
  114. ->with(array('id' => 'test'))
  115. ->will($this->returnValue(array('test')));
  116. $source->expects($this->once())
  117. ->method('current')
  118. ->will($this->returnValue($row));
  119. $this->executable->setSource($source);
  120. $this->migration->expects($this->once())
  121. ->method('getProcessPlugins')
  122. ->will($this->returnValue(array()));
  123. $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
  124. $destination->expects($this->once())
  125. ->method('import')
  126. ->with($row, array('test'))
  127. ->will($this->returnValue(TRUE));
  128. $this->migration
  129. ->method('getDestinationPlugin')
  130. ->willReturn($destination);
  131. $this->idMap->expects($this->never())
  132. ->method('saveIdMapping');
  133. $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
  134. }
  135. /**
  136. * Tests the import method with a valid row.
  137. */
  138. public function testImportWithValidRowNoDestinationValues() {
  139. $source = $this->getMockSource();
  140. $row = $this->getMockBuilder('Drupal\migrate\Row')
  141. ->disableOriginalConstructor()
  142. ->getMock();
  143. $row->expects($this->once())
  144. ->method('getSourceIdValues')
  145. ->will($this->returnValue(array('id' => 'test')));
  146. $source->expects($this->once())
  147. ->method('current')
  148. ->will($this->returnValue($row));
  149. $this->executable->setSource($source);
  150. $this->migration->expects($this->once())
  151. ->method('getProcessPlugins')
  152. ->will($this->returnValue(array()));
  153. $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
  154. $destination->expects($this->once())
  155. ->method('import')
  156. ->with($row, array('test'))
  157. ->will($this->returnValue(array()));
  158. $this->migration
  159. ->method('getDestinationPlugin')
  160. ->willReturn($destination);
  161. $this->idMap->expects($this->once())
  162. ->method('saveIdMapping')
  163. ->with($row, array(), MigrateIdMapInterface::STATUS_FAILED, NULL);
  164. $this->idMap->expects($this->once())
  165. ->method('messageCount')
  166. ->will($this->returnValue(0));
  167. $this->idMap->expects($this->once())
  168. ->method('saveMessage');
  169. $this->idMap->expects($this->once())
  170. ->method('lookupDestinationId')
  171. ->with(array('id' => 'test'))
  172. ->will($this->returnValue(array('test')));
  173. $this->message->expects($this->once())
  174. ->method('display')
  175. ->with('New object was not saved, no error provided');
  176. $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
  177. }
  178. /**
  179. * Tests the import method with a thrown MigrateException.
  180. *
  181. * The MigrationException in this case is being thrown from the destination.
  182. */
  183. public function testImportWithValidRowWithDestinationMigrateException() {
  184. $exception_message = $this->getRandomGenerator()->string();
  185. $source = $this->getMockSource();
  186. $row = $this->getMockBuilder('Drupal\migrate\Row')
  187. ->disableOriginalConstructor()
  188. ->getMock();
  189. $row->expects($this->once())
  190. ->method('getSourceIdValues')
  191. ->will($this->returnValue(array('id' => 'test')));
  192. $source->expects($this->once())
  193. ->method('current')
  194. ->will($this->returnValue($row));
  195. $this->executable->setSource($source);
  196. $this->migration->expects($this->once())
  197. ->method('getProcessPlugins')
  198. ->will($this->returnValue(array()));
  199. $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
  200. $destination->expects($this->once())
  201. ->method('import')
  202. ->with($row, array('test'))
  203. ->will($this->throwException(new MigrateException($exception_message)));
  204. $this->migration
  205. ->method('getDestinationPlugin')
  206. ->willReturn($destination);
  207. $this->idMap->expects($this->once())
  208. ->method('saveIdMapping')
  209. ->with($row, array(), MigrateIdMapInterface::STATUS_FAILED, NULL);
  210. $this->idMap->expects($this->once())
  211. ->method('saveMessage');
  212. $this->idMap->expects($this->once())
  213. ->method('lookupDestinationId')
  214. ->with(array('id' => 'test'))
  215. ->will($this->returnValue(array('test')));
  216. $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
  217. }
  218. /**
  219. * Tests the import method with a thrown MigrateException.
  220. *
  221. * The MigrationException in this case is being thrown from a process plugin.
  222. */
  223. public function testImportWithValidRowWithProcesMigrateException() {
  224. $exception_message = $this->getRandomGenerator()->string();
  225. $source = $this->getMockSource();
  226. $row = $this->getMockBuilder('Drupal\migrate\Row')
  227. ->disableOriginalConstructor()
  228. ->getMock();
  229. $row->expects($this->once())
  230. ->method('getSourceIdValues')
  231. ->willReturn(array('id' => 'test'));
  232. $source->expects($this->once())
  233. ->method('current')
  234. ->willReturn($row);
  235. $this->executable->setSource($source);
  236. $this->migration->expects($this->once())
  237. ->method('getProcessPlugins')
  238. ->willThrowException(new MigrateException($exception_message));
  239. $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
  240. $destination->expects($this->never())
  241. ->method('import');
  242. $this->migration
  243. ->method('getDestinationPlugin')
  244. ->willReturn($destination);
  245. $this->idMap->expects($this->once())
  246. ->method('saveIdMapping')
  247. ->with($row, array(), MigrateIdMapInterface::STATUS_FAILED, NULL);
  248. $this->idMap->expects($this->once())
  249. ->method('saveMessage');
  250. $this->idMap->expects($this->never())
  251. ->method('lookupDestinationId');
  252. $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
  253. }
  254. /**
  255. * Tests the import method with a regular Exception being thrown.
  256. */
  257. public function testImportWithValidRowWithException() {
  258. $exception_message = $this->getRandomGenerator()->string();
  259. $source = $this->getMockSource();
  260. $row = $this->getMockBuilder('Drupal\migrate\Row')
  261. ->disableOriginalConstructor()
  262. ->getMock();
  263. $row->expects($this->once())
  264. ->method('getSourceIdValues')
  265. ->will($this->returnValue(array('id' => 'test')));
  266. $source->expects($this->once())
  267. ->method('current')
  268. ->will($this->returnValue($row));
  269. $this->executable->setSource($source);
  270. $this->migration->expects($this->once())
  271. ->method('getProcessPlugins')
  272. ->will($this->returnValue(array()));
  273. $destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
  274. $destination->expects($this->once())
  275. ->method('import')
  276. ->with($row, array('test'))
  277. ->will($this->throwException(new \Exception($exception_message)));
  278. $this->migration
  279. ->method('getDestinationPlugin')
  280. ->willReturn($destination);
  281. $this->idMap->expects($this->once())
  282. ->method('saveIdMapping')
  283. ->with($row, array(), MigrateIdMapInterface::STATUS_FAILED, NULL);
  284. $this->idMap->expects($this->once())
  285. ->method('saveMessage');
  286. $this->idMap->expects($this->once())
  287. ->method('lookupDestinationId')
  288. ->with(array('id' => 'test'))
  289. ->will($this->returnValue(array('test')));
  290. $this->message->expects($this->once())
  291. ->method('display')
  292. ->with($exception_message);
  293. $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
  294. }
  295. /**
  296. * Tests the processRow method.
  297. */
  298. public function testProcessRow() {
  299. $expected = array(
  300. 'test' => 'test destination',
  301. 'test1' => 'test1 destination'
  302. );
  303. foreach ($expected as $key => $value) {
  304. $plugins[$key][0] = $this->getMock('Drupal\migrate\Plugin\MigrateProcessInterface');
  305. $plugins[$key][0]->expects($this->once())
  306. ->method('getPluginDefinition')
  307. ->will($this->returnValue(array()));
  308. $plugins[$key][0]->expects($this->once())
  309. ->method('transform')
  310. ->will($this->returnValue($value));
  311. }
  312. $this->migration->expects($this->once())
  313. ->method('getProcessPlugins')
  314. ->with(NULL)
  315. ->will($this->returnValue($plugins));
  316. $row = new Row();
  317. $this->executable->processRow($row);
  318. foreach ($expected as $key => $value) {
  319. $this->assertSame($row->getDestinationProperty($key), $value);
  320. }
  321. $this->assertSame(count($row->getDestination()), count($expected));
  322. }
  323. /**
  324. * Tests the processRow method with an empty pipeline.
  325. */
  326. public function testProcessRowEmptyPipeline() {
  327. $this->migration->expects($this->once())
  328. ->method('getProcessPlugins')
  329. ->with(NULL)
  330. ->will($this->returnValue(array('test' => array())));
  331. $row = new Row();
  332. $this->executable->processRow($row);
  333. $this->assertSame($row->getDestination(), array());
  334. }
  335. /**
  336. * Returns a mock migration source instance.
  337. *
  338. * @return \Drupal\migrate\Plugin\MigrateSourceInterface|\PHPUnit_Framework_MockObject_MockObject
  339. * The mocked migration source.
  340. */
  341. protected function getMockSource() {
  342. $iterator = $this->getMock('\Iterator');
  343. $class = 'Drupal\migrate\Plugin\migrate\source\SourcePluginBase';
  344. $source = $this->getMockBuilder($class)
  345. ->disableOriginalConstructor()
  346. ->setMethods(get_class_methods($class))
  347. ->getMockForAbstractClass();
  348. $source->expects($this->once())
  349. ->method('rewind')
  350. ->will($this->returnValue(TRUE));
  351. $source->expects($this->any())
  352. ->method('initializeIterator')
  353. ->will($this->returnValue([]));
  354. $source->expects($this->any())
  355. ->method('valid')
  356. ->will($this->onConsecutiveCalls(TRUE, FALSE));
  357. return $source;
  358. }
  359. }