diff --git a/core/modules/search/src/Plugin/migrate/destination/EntitySearchPage.php b/core/modules/search/src/Plugin/migrate/destination/EntitySearchPage.php index c53cf97..2e63dbf 100644 --- a/core/modules/search/src/Plugin/migrate/destination/EntitySearchPage.php +++ b/core/modules/search/src/Plugin/migrate/destination/EntitySearchPage.php @@ -3,9 +3,12 @@ namespace Drupal\search\Plugin\migrate\destination; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\migrate\Plugin\migrate\destination\EntityConfigBase; +use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Row; -use Drupal\search\Entity\SearchPage; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * @MigrateDestination( @@ -15,14 +18,67 @@ class EntitySearchPage extends EntityConfigBase { /** + * The module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * The search page. + * + * @var \Drupal\search\Entity\SearchPage + */ + protected $searchPage; + + /** + * Construct a new entity. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param MigrationInterface $migration + * The migration. + * @param EntityStorageInterface $storage + * The storage for this entity type. + * @param array $bundles + * The list of bundles this entity type has. + * @param ModuleHandlerInterface $module_handler + * The module handler. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, ModuleHandlerInterface $module_handler) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles); + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { + $entity_type_id = static::getEntityTypeId($plugin_id); + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $migration, + $container->get('entity.manager')->getStorage($entity_type_id), + array_keys($container->get('entity.manager')->getBundleInfo($entity_type_id)), + $container->get('module_handler') + ); + } + + /** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { - if (\Drupal::moduleHandler()->moduleExists($row->getDestinationProperty('module'))) { + if ($this->moduleHandler->moduleExists($row->getDestinationProperty('module'))) { parent::import($row, $old_destination_id_values); $id = $row->getDestinationProperty('id'); /** @var \Drupal\search\Entity\SearchPage $search_page */ - $search_page = SearchPage::load($id); + $search_page = $this->storage->load($id); if ($row->getDestinationProperty('status')) { $search_page->enable()->save(); } @@ -44,7 +100,7 @@ public function import(Row $row, array $old_destination_id_values = array()) { */ protected function updateEntity(EntityInterface $entity, Row $row) { $entity->setPlugin($row->getDestinationProperty('plugin')); - // The user_search plugin does not have a setConfiguraion method. + // The user_search plugin does not have a setConfiguraion() method. if (method_exists($entity->getPlugin(), 'setConfiguration')) { $entity->getPlugin()->setConfiguration($row->getDestinationProperty('configuration')); } diff --git a/core/modules/search/src/Plugin/migrate/source/d6/SearchPage.php b/core/modules/search/src/Plugin/migrate/source/d6/SearchPage.php index 6b3693e..a496af9 100644 --- a/core/modules/search/src/Plugin/migrate/source/d6/SearchPage.php +++ b/core/modules/search/src/Plugin/migrate/source/d6/SearchPage.php @@ -1,16 +1,8 @@ variables = $this->configuration['variables']; - } - - /** - * {@inheritdoc} - */ - protected function initializeIterator() { - return new \ArrayIterator($this->values()); - } +class SearchPage extends Variable { /** * Return the values of the variables specified in the plugin configuration. @@ -52,10 +22,7 @@ protected function initializeIterator() { * Only those values are returned that are actually in the database. */ protected function values() { - $rankings = array_map('unserialize', $this->prepareQuery()->execute()->fetchAllKeyed()); - $tmp['module'] = 'node'; - $values = array_merge($tmp, $rankings); - return [$values]; + return array_merge(['module' => 'node'], parent::values()); } /** @@ -70,25 +37,9 @@ public function fields() { /** * {@inheritdoc} */ - public function query() { - return $this->select('variable', 'v') - ->fields('v', array('name', 'value')) - ->condition('name', $this->configuration['variables'], 'IN'); - } - - /** - * {@inheritdoc} - */ public function getIds() { $ids['module']['type'] = 'string'; return $ids; } - /** - * {@inheritdoc} - */ - public function count() { - return $this->initializeIterator()->count(); - } - } diff --git a/core/modules/search/src/Plugin/migrate/source/d7/SearchPage.php b/core/modules/search/src/Plugin/migrate/source/d7/SearchPage.php index a73069b..56f5fee 100644 --- a/core/modules/search/src/Plugin/migrate/source/d7/SearchPage.php +++ b/core/modules/search/src/Plugin/migrate/source/d7/SearchPage.php @@ -1,17 +1,9 @@ variables = $this->configuration['variables']; - } +class SearchPage extends Variable { /** * {@inheritdoc} @@ -59,7 +36,7 @@ protected function values() { $search_active_modules = $this->variableGet('search_active_modules', ''); foreach ($search_active_modules as $module => $status) { if (($module === 'user') || ($module === 'node')) { - // Create an ID field so we can record migration in the map table. + // Create an ID field so we can record migration in the map table. $tmp = [ 'module' => $module, 'status' => $status, @@ -86,17 +63,9 @@ public function fields() { /** * {@inheritdoc} */ - public function query() { - return $this->select('variable', 'v') - ->fields('v', array('name', 'value')) - ->condition('name', $this->variables, 'IN'); - } - - /** - * {@inheritdoc} - */ public function getIds() { $ids['module']['type'] = 'string'; + $ids['status']['type'] = 'string'; return $ids; } diff --git a/core/modules/search/tests/src/Kernel/Migrate/d7/MigrateSearchPageTest.php b/core/modules/search/tests/src/Kernel/Migrate/d7/MigrateSearchPageTest.php index 7b09ef2..6010059 100644 --- a/core/modules/search/tests/src/Kernel/Migrate/d7/MigrateSearchPageTest.php +++ b/core/modules/search/tests/src/Kernel/Migrate/d7/MigrateSearchPageTest.php @@ -7,7 +7,7 @@ use Drupal\search\Entity\SearchPage; /** - * Migrate search page status and settings. + * Tests migration of search page status and settings. * * @group migrate_drupal_7 */ @@ -32,29 +32,22 @@ protected function setUp() { * * @param string $id * The expected search page ID. - * @param boolean $present - * True if the entity exists. * @param string $path - * The expected image style label. + * The expected path of the search page. * @param boolean $status * The expected status of the search page. * @param array $expected_config * An array of expected configuration for the search page. */ - protected function assertEntity($id, $present, $path, $status, array $expected_config = NULL) { + protected function assertEntity($id, $path, $status = FALSE, array $expected_config = NULL) { /** @var \Drupal\search\Entity\SearchPage $search_page */ $search_page = SearchPage::load($id); - if ($present) { - $this->assertIdentical($id, $search_page->id()); - $this->assertIdentical($path, $search_page->getPath()); - $this->assertIdentical($search_page->status(), $status); - if ($expected_config) { - $configuration = $search_page->getPlugin()->getConfiguration(); - $this->assertIdentical($expected_config, $configuration); - } - } - else { - $this->assertNull($search_page); + $this->assertSame($id, $search_page->id()); + $this->assertSame($path, $search_page->getPath()); + $this->assertSame($search_page->status(), $status); + if (isset($expected_config)) { + $configuration = $search_page->getPlugin()->getConfiguration(); + $this->assertSame($expected_config, $configuration); } } @@ -76,8 +69,8 @@ public function testSearchPage() { 'views' => 0, ] ]; - $this->assertEntity('node_search', TRUE, 'node', TRUE, $configuration); - $this->assertEntity('user_search', TRUE, 'user', FALSE, NULL); + $this->assertEntity('node_search', 'node', TRUE, $configuration); + $this->assertEntity('user_search', 'user'); // Test that we can re-import using the EntitySearchPage destination. Database::getConnection('default', 'migrate') @@ -92,7 +85,7 @@ public function testSearchPage() { $migration->getIdMap()->prepareUpdate(); $this->executeMigration($migration); $configuration['rankings']['comments'] = 4; - $this->assertEntity('node_search', TRUE, 'node', TRUE, $configuration); + $this->assertEntity('node_search', 'node', TRUE, $configuration); } /** @@ -103,8 +96,8 @@ public function testModuleExists() { $this->installConfig(['search']); $this->executeMigration('d7_search_page'); - $this->assertEntity('node_search', FALSE, 'node', TRUE, NULL); - $this->assertEntity('user_search', TRUE, 'user', FALSE, NULL); + $this->assertNull(SearchPage::load('node_search')); + $this->assertEntity('user_search', 'user'); } /** @@ -118,6 +111,6 @@ public function testUserSearchCreate() { $search_page->delete(); $this->executeMigration('d7_search_page'); - $this->assertEntity('user_search', TRUE, 'user', FALSE, NULL); + $this->assertEntity('user_search', 'user'); } } diff --git a/core/modules/search/tests/src/Unit/Plugin/migrate/source/d6/SearchPageTest.php b/core/modules/search/tests/src/Unit/Plugin/migrate/source/d6/SearchPageTest.php index ba6ebb6..eace016 100644 --- a/core/modules/search/tests/src/Unit/Plugin/migrate/source/d6/SearchPageTest.php +++ b/core/modules/search/tests/src/Unit/Plugin/migrate/source/d6/SearchPageTest.php @@ -1,68 +1,61 @@ 'test', - 'source' => [ - 'plugin' => 'd7_search_page', - 'variables' => ['node_rank_comments', 'node_rank_promote'], - ] - ]; + public static $modules = ['search', 'migrate_drupal']; /** - * Expected results from the source. + * {@inheritdoc} */ - protected $expectedResults = [ - [ - 'module' => 'node', - 'node_rank_comments' => '5', - 'node_rank_promote' => '1', - ], - ]; - - /** - * Database contents. - */ - protected $databaseContents = [ - 'variable' => [ - [ - 'name' => 'node_rank_comments', - 'value' => 's:1:"5";', + public function providerSource() { + $tests[0]['source_data'] = [ + 'variable' => [ + [ + 'name' => 'node_rank_comments', + 'value' => 's:1:"5";', + ], + [ + 'name' => 'node_rank_promote', + 'value' => 's:1:"1";', + ], ], + 'system' => [ + [ + 'name' => 'node', + 'type' => 'module', + 'status' => '1', + ] + ] + ]; + + $tests[0]['expected_data'] = [ [ - 'name' => 'node_rank_promote', - 'value' => 's:1:"1";', + 'module' => 'node', + 'node_rank_comments' => '5', + 'node_rank_promote' => '1', ], - ], - 'system' => [ - [ - 'name' => 'node', - 'type' => 'module', - 'status' => '1', - ] - ] - ]; + ]; + + $tests[0]['expected_count'] = NULL; + + $tests[0]['configuration'] = [ + 'variables' => ['node_rank_comments', 'node_rank_promote'], + ]; + + return $tests; + } } diff --git a/core/modules/search/tests/src/Unit/Plugin/migrate/source/d7/SearchPageTest.php b/core/modules/search/tests/src/Unit/Plugin/migrate/source/d7/SearchPageTest.php index 32a1ebc..1128929 100644 --- a/core/modules/search/tests/src/Unit/Plugin/migrate/source/d7/SearchPageTest.php +++ b/core/modules/search/tests/src/Unit/Plugin/migrate/source/d7/SearchPageTest.php @@ -1,84 +1,77 @@ 'test', - 'source' => [ - 'plugin' => 'd7_search_page', - 'variables' => ['node_rank_comments', 'node_rank_promote'], - ] - ]; +class SearchPageTest extends MigrateSqlSourceTestBase { /** - * Expected results from the source. + * {@inheritdoc} */ - protected $expectedResults = [ - [ - 'module' => 'node', - 'status' => 'node', - 'module_exists' => false, - 'node_rank_comments' => '5', - 'node_rank_promote' => '1', - ], - [ - 'module' => 'user', - 'status' => 0, - 'module_exists' => true, - ] - ]; + public static $modules = ['search', 'migrate_drupal']; /** - * Database contents. + * {@inheritdoc} */ - protected $databaseContents = [ - 'variable' => [ - [ - 'name' => 'search_active_modules', - 'value' => 'a:2:{s:4:"node";s:4:"node";s:4:"user";i:0;}', + public function providerSource() { + $tests[0]['source_data'] = [ + 'variable' => [ + [ + 'name' => 'search_active_modules', + 'value' => 'a:2:{s:4:"node";s:4:"node";s:4:"user";i:0;}', + ], + [ + 'name' => 'node_rank_comments', + 'value' => 's:1:"5";', + ], + [ + 'name' => 'node_rank_promote', + 'value' => 's:1:"1";', + ], ], - [ - 'name' => 'node_rank_comments', - 'value' => 's:1:"5";', + 'system' => [ + [ + 'name' => 'node', + 'type' => 'module', + 'status' => '0', + ], + [ + 'name' => 'user', + 'type' => 'module', + 'status' => '1', + ], ], + ]; + + $tests[0]['expected_data'] = [ [ - 'name' => 'node_rank_promote', - 'value' => 's:1:"1";', + 'module' => 'node', + 'status' => 'node', + 'module_exists' => FALSE, + 'node_rank_comments' => '5', + 'node_rank_promote' => '1', ], - ], - 'system' => [ [ - 'name' => 'node', - 'type' => 'module', - 'status' => '0', + 'module' => 'user', + 'status' => 0, + 'module_exists' => TRUE, ], - [ - 'name' => 'user', - 'type' => 'module', - 'status' => '1', - ] - ] - ]; + ]; + + $tests[0]['expected_count'] = NULL; + + $tests[0]['configuration'] = [ + 'variables' => ['node_rank_comments', 'node_rank_promote'], + ]; + + return $tests; + } }