Skip to content
Snippets Groups Projects
Commit dfefab5e authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2499793 by mikeryan, phenaproxima: Several migrate_drupal migrations fatal error on count()

parent 515e6fbf
No related branches found
No related tags found
No related merge requests found
<?php
/**
* @file
* Contains \Drupal\migrate\Plugin\migrate\source\DummyQueryTrait.
*/
namespace Drupal\migrate\Plugin\migrate\source;
/**
* Trait providing a dummy select query object for source plugins based on
* SqlBase which override initializeIterator() to obtain their data from other
* SqlBase services instead of a direct query. This ensures that query() returns
* a valid object, even though it isn't used for iteration.
*/
trait DummyQueryTrait {
/**
* @return \Drupal\Core\Database\Query\SelectInterface
*/
public function query() {
// Pass an arbritrary table name - the query should never be executed anyway.
$query = $this->select(uniqid(), 's')
->range(0, 1);
$query->addExpression('1');
return $query;
}
}
......@@ -45,6 +45,13 @@ abstract class MigrateTestBase extends KernelTestBase implements MigrateMessageI
*/
protected $migrateMessages;
/**
* The primary migration being tested.
*
* @var \Drupal\migrate\Entity\MigrationInterface
*/
protected $migration;
public static $modules = array('migrate');
/**
......@@ -132,12 +139,15 @@ protected function prepareMigrations(array $id_mappings) {
*/
protected function executeMigration($migration) {
if (is_string($migration)) {
$migration = Migration::load($migration);
$this->migration = Migration::load($migration);
}
else {
$this->migration = $migration;
}
if ($this instanceof MigrateDumpAlterInterface) {
static::migrateDumpAlter($this);
}
(new MigrateExecutable($migration, $this))->import();
(new MigrateExecutable($this->migration, $this))->import();
}
/**
......
......@@ -58,6 +58,13 @@ abstract class MigrateSqlSourceTestCase extends MigrateTestCase {
*/
protected $expectedResults = array();
/**
* Expected count of source rows.
*
* @var int
*/
protected $expectedCount = 0;
/**
* The source plugin instance under test.
*
......@@ -97,6 +104,7 @@ protected function setUp() {
->method('getSourcePlugin')
->will($this->returnValue($plugin));
$this->source = $plugin;
$this->expectedCount = count($this->expectedResults);
}
/**
......@@ -106,6 +114,13 @@ public function testRetrieval() {
$this->queryResultTest($this->source, $this->expectedResults);
}
/**
* Test the source returns the row count expected.
*/
public function testSourceCount() {
$this->assertEquals($this->source->count(), $this->expectedCount);
}
/**
* @param \Drupal\migrate\Row $row
* @param string $key
......
......@@ -8,6 +8,7 @@
namespace Drupal\migrate_drupal\Plugin\migrate\source\d6;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Drupal\migrate\Plugin\migrate\source\DummyQueryTrait;
/**
* @MigrateSource(
......@@ -16,6 +17,8 @@
*/
class CommentVariable extends DrupalSqlBase {
use DummyQueryTrait;
/**
* {@inheritdoc}
*/
......@@ -92,13 +95,6 @@ protected function commentPrefixes() {
);
}
/**
* {@inheritdoc}
*/
public function query() {
// Nothing to do here.
}
/**
* {@inheritdoc}
*/
......
......@@ -8,6 +8,7 @@
namespace Drupal\migrate_drupal\Plugin\migrate\source\d6;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Drupal\migrate\Plugin\migrate\source\DummyQueryTrait;
/**
* Drupal 6 upload instance source from database.
......@@ -19,6 +20,8 @@
*/
class UploadInstance extends DrupalSqlBase {
use DummyQueryTrait;
/**
* {@inheritdoc}
*/
......@@ -60,13 +63,6 @@ public function getIds() {
);
}
/**
* {@inheritdoc}
*/
public function query() {
// Nothing needed here.
}
/**
* {@inheritdoc}
*/
......@@ -78,4 +74,11 @@ public function fields() {
);
}
/**
* {@inheritdoc}
*/
public function count() {
return count($this->initializeIterator());
}
}
......@@ -8,6 +8,7 @@
namespace Drupal\migrate_drupal\Plugin\migrate\source\d6;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Drupal\migrate\Plugin\migrate\source\DummyQueryTrait;
/**
* Drupal 6 user picture field instance source.
......@@ -20,6 +21,8 @@
*/
class UserPictureInstance extends DrupalSqlBase {
use DummyQueryTrait;
/**
* {@inheritdoc}
*/
......@@ -33,6 +36,15 @@ public function initializeIterator() {
)));
}
/**
* {@inheritdoc}
*/
public function count() {
// This source provides a single row, corresponding to a single picture
// field to be added to the user entity.
return 1;
}
/**
* {@inheritdoc}
*/
......@@ -52,11 +64,4 @@ public function getIds() {
return $ids;
}
/**
* {@inheritdoc}
*/
public function query() {
// Nothing to do here.
}
}
......@@ -10,6 +10,8 @@
use Drupal\migrate\Tests\MigrateTestBase;
use Drupal\migrate\Entity\Migration;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\Core\Database\Query\SelectInterface;
/**
* Base class for Drupal migration tests.
......@@ -74,4 +76,21 @@ protected function installMigrations($version) {
}
}
}
/**
* Test that the source plugin provides a valid query and a valid count.
*/
public function testSourcePlugin() {
if (isset($this->migration)) {
$source = $this->migration->getSourcePlugin();
// Make sure a SqlBase source has a valid query.
if ($source instanceof SqlBase) {
/** @var SqlBase $source */
$this->assertTrue($source->query() instanceof SelectInterface, 'SQL source plugin has valid query');
}
// Validate that any source returns a valid count.
$this->assertTrue(is_numeric($source->count()), 'Source plugin returns valid count');
}
}
}
......@@ -23,5 +23,8 @@ protected function setUp() {
$this->expectedResults = array_values(array_filter($this->expectedResults, function($result) {
return $result['vid'] == 5;
}));
// We know there are two rows with vid == 5.
$this->expectedCount = 2;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment