Problem/Motivation

Follow-up to #2994904-37: Convert query('SELECT ... FROM {xxx}') to select('xxx')->... in tests
Core should not use direct db calls to entity tables because they are managed by the table mapper entity API.

Proposed resolution

All db_query() and similar should be replaced with \Drupal::entityQuery() or with injected entity storage, and updates/deletes should be done via the Entity API.

Remaining tasks

replace all usage

User interface changes

no

API changes

no

Data model changes

no

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

andypost created an issue. See original summary.

mondrake’s picture

Status: Active » Needs review
FileSize
1.95 KB

Just a very first patch with one possible conversion, to see how it looks.

It's for AggregatorCronTest.

Looking at the remaining code, I now see

    $connection = Database::getConnection();
    $connection->update('aggregator_feed')
      ->condition('fid', $feed->id())
      ->fields([
        'queued' => REQUEST_TIME,
      ])
      ->execute();
    ...
    $connection->update('aggregator_feed')
      ->condition('fid', $feed->id())
      ->fields([
        'queued' => 0,
      ])
      ->execute();

and wonder if this is correct? Shouldn't this be managed via entity instead?

mondrake’s picture

Addressed all the 'count' queries from #2994904-39: Convert query('SELECT ... FROM {xxx}') to select('xxx')->... in tests,

core/modules/aggregator/tests/src/Functional/AggregatorCronTest.php
core/modules/aggregator/tests/src/Functional/DeleteFeedTest.php
core/modules/aggregator/tests/src/Functional/ImportOpmlTest.php
core/modules/taxonomy/tests/src/Functional/TermIndexTest.php

There are way more to do...

andypost’s picture

Interesting point, I thought to start with aggregator test base)

Btw IMO it makes sense to rework tests to use entity api, ircc there was isdues about it like replacement of entity_create()

andypost’s picture

Kinda #2928930: Properly deprecate entity_create() and remove all occurrences
But it looks like there should be a meta about to rewamp all entity usage)

andypost’s picture

Status: Needs review » Needs work

The last submitted patch, 3: 3012599-3.patch, failed testing. View results

mondrake’s picture

Status: Needs work » Needs review
FileSize
11.07 KB
14.6 KB

Trying going a bit more the Entity API way instead of the db way in the aggregator tests.

taxonomy_index table is defined through the entity schema, but it's not an entity itself so reverting to use db calls.

Still experimenting here.

Status: Needs review » Needs work

The last submitted patch, 8: 3012599-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mondrake’s picture

Status: Needs work » Needs review
FileSize
14.71 KB
3 KB

Try this.

mondrake’s picture

Re. #4:

it makes sense to rework tests to use entity api

then I think we need a meta and children since reworking tests like that is much bigger than just converting db calls.

Potential tests affected:
core\modules\aggregator\tests\src\Functional\FeedParserTest.php
core\modules\aggregator\tests\src\Functional\UpdateFeedItemTest.php
core\modules\block_content\tests\src\Functional\BlockContentSaveTest.php
core\modules\block_content\tests\src\Functional\BlockContentTranslationUITest.php
core\modules\content_moderation\tests\src\Functional\DefaultContentModerationStateRevisionUpdateTest.php
core\modules\file\tests\src\Functional\SaveUploadFormTest.php
core\modules\file\tests\src\Functional\SaveUploadTest.php
core\modules\file\tests\src\FunctionalJavascript\FileManagedFileElementTest.php
core\modules\node\tests\src\Functional\NodeAccessBaseTableTest.php
core\modules\node\tests\src\Functional\NodeAccessRecordsTest.php
core\modules\node\tests\src\Functional\NodeRevisionPermissionsTest.php
core\modules\node\tests\src\Functional\NodeRevisionsAllTest.php
core\modules\node\tests\src\Functional\NodeRevisionsTest.php
core\modules\node\tests\src\Functional\NodeTranslationUITest.php
core\modules\path\tests\src\Functional\PathTaxonomyTermTest.php
core\modules\taxonomy\tests\src\Functional\TermTranslationUITest.php
core\modules\user\tests\src\Kernel\UserInstallTest.php

mondrake’s picture

Assigned: Unassigned » mondrake
Status: Needs review » Needs work

I’ll finish converting the aggregator module.

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Needs work » Needs review
Issue tags: +Needs issue summary update
FileSize
15.41 KB
17.46 KB

This is meant to convert all of direct db access (query/delete/update) to aggregator_feed and aggregator_item tables, to Entity API and Entity Query API.

Reverted the changes for TermIndexTest that no longer belong here.

I'd rather change this issue title to point to conversion of the two entities, and create a new meta of which this one should be a child.

Status: Needs review » Needs work

The last submitted patch, 13: 3012599-13.patch, failed testing. View results

mondrake’s picture

Title: Replace all direct queries to entity tables with entity query » Replace all db calls to aggregator_feed and aggregator_item tables with Entity APIs
Assigned: Unassigned » mondrake
Issue summary: View changes
Issue tags: -Needs issue summary update

Updated issue title and summary.

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Needs work » Needs review
FileSize
17.5 KB
3.05 KB

Addressing failures.

Status: Needs review » Needs work

The last submitted patch, 16: 3012599-16.patch, failed testing. View results

mondrake’s picture

Status: Needs work » Needs review
FileSize
17.54 KB
1.4 KB

One more. If this turns out green, it'd be ready for review.

mondrake’s picture

voleger’s picture

Status: Needs review » Reviewed & tested by the community

Looks good.

alexpott’s picture

Category: Bug report » Task
Status: Reviewed & tested by the community » Fixed

Can someone open a followup to remove queries against the node table in:

  • \Drupal\aggregator\Tests\AggregatorTestBase::getDefaultFeedItemCount
  • \Drupal\Tests\aggregator\Functional\AggregatorTestBase::getDefaultFeedItemCount

Committed 1da1c2b and pushed to 8.7.x. Thanks!

  • alexpott committed 1da1c2b on 8.7.x
    Issue #3012599 by mondrake, andypost: Replace all db calls to...
alexpott’s picture

Backported to 8.6.x too as this is test only change. Committed b7ef221 and pushed to 8.6.x. Thanks!

  • alexpott committed b7ef221 on 8.6.x
    Issue #3012599 by mondrake, andypost: Replace all db calls to...
andypost’s picture

mondrake’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.