Problem/Motivation

Core should not use direct db calls to entity tables because they are managed by the table mapper entity API.

Proposed resolution

All ::query() call 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

mondrake created an issue. See original summary.

mondrake’s picture

mondrake’s picture

Status: Active » Needs review
mondrake’s picture

Actually this one is probably more in tune with the objectives of the test.

Edit: incorrect, this is not the logic of the test.

Status: Needs review » Needs work

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

mondrake’s picture

+++ b/core/modules/block_content/tests/src/Functional/BlockContentTranslationUITest.php
@@ -165,13 +165,15 @@ public function testDisabledBundle() {
-    // Make sure that only a single row was inserted into the block table.
-    $rows = db_query('SELECT * FROM {block_content_field_data} WHERE id = :id', [':id' => $enabled_block_content->id()])->fetchAll();
-    $this->assertEqual(1, count($rows));

I am not really sure what this is meant to test, so it's unclear how to convert this query. Looking at earlier versions, the code used to be

+    // Make sure that only a single row was inserted into the
+    // {content_translation} table.
+    $rows = db_query('SELECT * FROM {content_translation}')->fetchAll();
+    $this->assertEqual(1, count($rows));
+    $this->assertEqual($enabled_block_content->id(), reset($rows)->entity_id);

then it was changed to the current version in #1916790: Convert translation metadata into regular entity fields. The first patch doing the change is @ #1916790-76: Convert translation metadata into regular entity fields, but I cannot figure out what the intention was.

goodboy’s picture

As I understand it, this test does not verify what is declared. $enabled_block_content->id() is not equal $disabled_block_content->id() and for enabled block the query will be return 1 anyway.

$rows = db_query('SELECT * FROM {block_content_field_data} WHERE id = :id', [':id' => $enabled_block_content->id()])->fetchAll();an
$this->assertEqual(1, count($rows)); 

And the last test checks correctly but it does not passed. If 4 years ago the current test was correct, we would see the moment when changes were made, after which the test would cease to pass, as is happening now. So it seems we catch the bug of https://www.drupal.org/project/drupal/issues/1916790 and we have to wait until this new test will be passed after the issue bug will be fixed.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

The test should be converted to using table mapping to get table name in this kind of testing to keep logic same

gigiabba’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#2875394: Replace all calls to db_query, which is deprecated

This issue should be closed.
Calls to db_query function are already replaced and pushed in 8.8 branch.
See https://www.drupal.org/project/drupal/issues/2875394#comment-13123165

gigiabba’s picture

-duplicated comment-

mondrake’s picture

Status: Closed (duplicate) » Needs work

@gigiabba nope it's a different thing - here we are talking about moving from ::query() to Entity query API. #2875394: Replace all calls to db_query, which is deprecated just replaced db_query with ::query without further consideration.

voleger’s picture

Issue tags: +Needs reroll
gigiabba’s picture

@mondrake so sorry! :)

vacho’s picture

Issue tags: -Needs reroll

This issue is out of time. Core doesn't have any call to db_query except at the tests that cover it and functions. So I think that this issue can be closed

voleger’s picture

Issue summary: View changes
Issue tags: +Needs reroll

Exactly this is a reason why we need a reroll on this issue. db_query functions were just replaced by the query method calls on the database connection instance. So the scope of this issue is still the same.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

neel24’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
2 KB

I've rerolled the patch from #4.

Status: Needs review » Needs work

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

hash6’s picture

Assigned: Unassigned » hash6
hash6’s picture

Below are my observations towards the implementation.

$rows = Database::getConnection()->query('SELECT * FROM {block_content_field_data} WHERE id = :id', [':id' => $enabled_block_content->id()])->fetchAll();

- Earlier code was fetching from block_content_field_data table with condition of block id which will always return value 1 since we are passing id of block created.

 $query = \Drupal::entityQuery('block_content')->condition('langcode','en','=')->count();

- Above code is added in the patch which will generate a count which shows the block having language English
- In the code hereafter, we have

    $disabled_block_content = $this->createBlockContent(FALSE, $bundle->id(), LanguageInterface::LANGCODE_NOT_SPECIFIED);

- Language Code when added for $disabled_block_content should not have langcode as english
- Adding langcode as en by default will always give the count to be 2
- hence i have added an addiational parameter langcode to the createBlockContent()
so that the first block will create a count of block with en as the language and second block created will have language not specified, which eventually passes the test.

hash6’s picture

Assigned: hash6 » Unassigned
Status: Needs work » Needs review
daffie’s picture

Status: Needs review » Needs work

The patch looks good. I have just 1 minor point:

+++ b/core/modules/block_content/tests/src/Functional/BlockContentTranslationUITest.php
@@ -91,17 +93,18 @@ public function getTranslatorPermissions() {
+  protected function createBlockContent($title = FALSE, $bundle = FALSE, $langcode = 'en') {

@@ -170,14 +173,13 @@ public function testDisabledBundle() {
+    $query = \Drupal::entityQuery('block_content')->condition('langcode','en','=')->count();
...
+    $disabled_block_content = $this->createBlockContent(FALSE, $bundle->id(), LanguageInterface::LANGCODE_NOT_SPECIFIED);

For all 3 places: use the same value. Either "en" or LanguageInterface::LANGCODE_NOT_SPECIFIED.

Neslee Canil Pinto’s picture

@daffie, will change it to en everywhere

daffie’s picture

As nobody seams to understand what the test Drupal\Tests\block_content\Functional\BlockContentTranslationUITest::testDisabledBundle() is supposed to test. Can we just remove the test?

Neslee Canil Pinto’s picture

FileSize
3.34 KB
1.25 KB
daffie’s picture

@Neslle: I think you can also delete the helper method createBlockContent(), when you delete testDisabledBundle().

Neslee Canil Pinto’s picture

daffie’s picture

+++ b/core/modules/block_content/tests/src/Functional/BlockContentTranslationUITest.php
@@ -6,6 +6,8 @@
+use Drupal\Core\Language\LanguageInterface;
+

These lines will then also not be needed. The same for use statements for Drupal\block_content\Entity\BlockContent and Drupal\Core\Database\Database.

Neslee Canil Pinto’s picture

daffie’s picture

Neslee Canil Pinto’s picture

Status: Needs work » Needs review
FileSize
3.38 KB

Patch for 9.1.x

daffie’s picture

Status: Needs review » Reviewed & tested by the community

As nobody seems to understand what the test Drupal\Tests\block_content\Functional\BlockContentTranslationUITest::testDisabledBundle() is supposed to test. Lets remove the test.

All code changes look good.
All database queries of the block_content module that can be changed, have been changed to an entity query.
For me it is RTBC.

@Neslee: Thank you for all of your patches.

andypost’s picture

It looks like testing block content with revisions disabled, but it looks strange

catch’s picture

Status: Reviewed & tested by the community » Needs review

Could we double check why Drupal\Tests\block_content\Functional\BlockContentTranslationUITest::testDisabledBundle() was added via git blame? Agreed it's an odd looking test that doesn't seem to match its documentation.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The removed test Drupal\Tests\block_content\Functional\BlockContentTranslationUITest::testDisabledBundle() was added in #1871772: Convert custom blocks to content entities. I think we can assume that all custom blocks are now content entities.

  • catch committed 701a778 on 9.1.x
    Issue #3015855 by Neslee Canil Pinto, mondrake, hash6, neel24, daffie,...

  • catch committed 219df57 on 9.0.x
    Issue #3015855 by Neslee Canil Pinto, mondrake, hash6, neel24, daffie,...
catch’s picture

Version: 9.1.x-dev » 9.0.x-dev
Component: database system » block_content.module
Status: Reviewed & tested by the community » Fixed

Committed 219df57 and pushed to 9.1.x, cherry-picked to 9.0.x. Thanks!

Status: Fixed » Closed (fixed)

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