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

Issue #2833060 by claudiu.cristea, matroskeen, heddn, dww, benjifisher,...

Issue #2833060 by claudiu.cristea, matroskeen, heddn, dww, benjifisher, alexpott, xurizaemon, liam morland, quietone, mikeryan, mikelutz, pfrenssen: SqlBase::prepareQuery() should be called also on count
parent c9212079
No related branches found
No related tags found
6 merge requests!11197Issue #3506427 by eduardo morales alberti: Remove responsive_image.ajax from hook,!5423Draft: Resolve #3329907 "Test2",!3478Issue #3337882: Deleted menus are not removed from content type config,!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!579Issue #2230909: Simple decimals fail to pass validation
Pipeline #418003 passed with warnings
Pipeline: drupal

#418014

    Pipeline: drupal

    #418009

      Pipeline: drupal

      #418005

        ......@@ -133,7 +133,7 @@ public static function create(ContainerInterface $container, array $configuratio
        * The query string.
        */
        public function __toString() {
        return (string) $this->query();
        return (string) $this->prepareQuery();
        }
        /**
        ......@@ -383,8 +383,13 @@ public function rewind(): void {
        }
        /**
        * Prepares query object to retrieve data from the source database.
        *
        * This method should not be called directly. It is called automatically from
        * SqlBase::prepareQuery().
        *
        * @return \Drupal\Core\Database\Query\SelectInterface
        * The query object.
        * A Select query object with the source data.
        */
        abstract public function query();
        ......@@ -392,7 +397,7 @@ abstract public function query();
        * Gets the source count using countQuery().
        */
        protected function doCount() {
        return (int) $this->query()->countQuery()->execute()->fetchField();
        return (int) $this->prepareQuery()->countQuery()->execute()->fetchField();
        }
        /**
        ......
        name: 'Migrate SQL prepare query test'
        type: module
        description: 'Provides a source plugin to test prepare query method.'
        package: Testing
        version: VERSION
        <?php
        declare(strict_types=1);
        namespace Drupal\migrate_sql_prepare_query_test\Plugin\migrate\source;
        use Drupal\migrate\Plugin\migrate\source\SqlBase;
        /**
        * Source plugin for prepare query test.
        *
        * @MigrateSource(
        * id = "test_sql_prepare_query"
        * )
        */
        class TestSqlPrepareQuery extends SqlBase {
        /**
        * {@inheritdoc}
        */
        public function query() {
        return $this->select('migrate_source_test')->fields('migrate_source_test');
        }
        /**
        * {@inheritdoc}
        */
        protected function prepareQuery() {
        $this->query = parent::prepareQuery();
        $this->query->condition('name', 'foo', '!=');
        return $this->query;
        }
        /**
        * {@inheritdoc}
        */
        public function getIds() {
        return ['id' => ['type' => 'integer']];
        }
        /**
        * {@inheritdoc}
        */
        public function fields() {
        return ['id' => 'ID', 'name' => 'Name'];
        }
        }
        ......@@ -9,6 +9,8 @@
        use Drupal\Core\Database\StatementInterface;
        use Drupal\migrate\Exception\RequirementsException;
        use Drupal\Core\Database\Database;
        use Drupal\migrate\MigrateExecutable;
        use Drupal\migrate\MigrateMessage;
        use Drupal\migrate\Plugin\migrate\source\SqlBase;
        use Drupal\migrate\Plugin\MigrationInterface;
        ......@@ -198,6 +200,52 @@ public static function highWaterDataProvider() {
        ];
        }
        /**
        * Tests prepare query method.
        */
        public function testPrepareQuery(): void {
        $this->prepareSourceData();
        $this->enableModules(['migrate_sql_prepare_query_test', 'entity_test']);
        /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
        $migration = $this->container->get('plugin.manager.migration')
        ->createStubMigration([
        'source' => ['plugin' => 'test_sql_prepare_query'],
        'process' => ['id' => 'id', 'name' => 'name'],
        'destination' => ['plugin' => 'entity:entity_test'],
        ]);
        // One item is excluded by the condition defined in the source plugin.
        // @see \Drupal\migrate_sql_prepare_query_test\Plugin\migrate\source\TestSqlPrepareQuery
        $count = $migration->getSourcePlugin()->count();
        $this->assertEquals(2, $count);
        // Run the migration and verify that the number of migrated items matches
        // the initial source count.
        (new MigrateExecutable($migration, new MigrateMessage()))->import();
        $this->assertEquals(2, $migration->getIdMap()->processedCount());
        }
        /**
        * Creates a custom source table and some sample data.
        */
        protected function prepareSourceData(): void {
        $this->sourceDatabase->schema()->createTable('migrate_source_test', [
        'fields' => [
        'id' => ['type' => 'int'],
        'name' => ['type' => 'varchar', 'length' => 32],
        ],
        ]);
        // Add some data in the table.
        $this->sourceDatabase->insert('migrate_source_test')
        ->fields(['id', 'name'])
        ->values(['id' => 1, 'name' => 'foo'])
        ->values(['id' => 2, 'name' => 'bar'])
        ->values(['id' => 3, 'name' => 'baz'])
        ->execute();
        }
        }
        /**
        ......
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Please register or to comment