Problem/Motivation

In Views on D7 there's code in views_many_to_one_helper as follows:

    while ($r_join->left_table != $base_table) {
      $r_join = views_get_table_join($r_join->left_table, $base_table);
    }

Problems can arise if $r_join is invalid for some reason, e.g. a table becomes invalid or the join definition is incorrect.

In D7 the solution was to just add a check that $r_join was valid.

In D9 this code was moved to ManyToOneHelper.

Steps to reproduce

Add a missing table to your Views data and try to add a relationship to that table
The new test coverage shows it best.

Proposed resolution

For D9 we want to make sure we are not just masking the problem, but if you end up in this situation, that means your Views data is corrupt. At that point we need to fail hard to make sure the problem gets fixed.
Throw an exception when severely broken Views data is encountered.

Remaining tasks

Review

User interface changes

None

API changes

None

Data model changes

We throw an exception when severely broken Views data is encountered instead of looping endlessly

Release notes snippet

No needed

Issue fork drupal-2928757

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

DamienMcKenna created an issue. See original summary.

dawehner’s picture

Thanks a ton for creating the issue!

lendude’s picture

Status: Active » Needs review
StatusFileSize
new3.05 KB
new4.04 KB

This is the equivalent of the D7 fix for D8. The failing test simulates a missing join.

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

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

+++ b/core/modules/views/tests/src/Unit/ManyToOneHelperTest.php
@@ -0,0 +1,94 @@
+    $join->leftTable = 'left_table';

What about calling it "non_existing_left_table". Given that should we also test what happens on a non existing right_table?

lendude’s picture

StatusFileSize
new1.08 KB
new4.06 KB

@dawehner yeah that makes sense. Changed the wording.

Adding a test for the right_table seems out of scope here, fancy enough to get some explicit testing for ManyToOneHelper as it is.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @Lendude, especially for pinging me!

alexpott’s picture

How does the infinite loop occur? The test-only patch errors on trying to get a property of a non-object. Are we just hiding an error with this change?

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

Setting back to "needs review" so someone can answer #9.

lendude’s picture

StatusFileSize
new5.61 KB
new8.21 KB

@alexpott you are right on both counts, we were just hiding the error and the test scenario wasn't the correct one to trigger the infinite loop.

Added a test for the infinite loop and instead of just quietly passing the error by, we now throw an exception when this happens, after all there is something seriously wrong when you run into this. But maybe that's a bit of overkill?

To test if we are inside a loop I've taken a page from \Drupal\views\Plugin\views\query\Sql::ensurePath which already protects against that.

dawehner’s picture

This is indeed much better!

+++ b/core/modules/views/src/Exception/InvalidViewsDataException.php
@@ -0,0 +1,10 @@
+/**
+ * Thrown when invalid views data is encountered.
+ */
+class InvalidViewsDataException extends \Exception {

Can we document somehow how to resolve this for people? Like a table pointing to itself?

lendude’s picture

StatusFileSize
new572 bytes
new8.37 KB

@dawehner yeah that makes sense, something like this?

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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.

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.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ranjith_kumar_k_u’s picture

StatusFileSize
new8.32 KB

Re-rolled #13 for 9.4.

alexpott’s picture

+++ b/core/modules/views/src/ManyToOneHelper.php
@@ -57,10 +58,19 @@ public function getField() {
+   * @param null $join
+   * @param null $alias
+   *
+   * @return string

These need to confirm to our documentation standards. So we need a comment explaining each one. Also I think $alias is a string and $join will be a \Drupal\views\Plugin\views\join\JoinPluginInterface

yogeshmpawar’s picture

StatusFileSize
new8.42 KB
new645 bytes

Resolved CS issues

Status: Needs review » Needs work

The last submitted patch, 23: 2928757-23.patch, failed testing. View results

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pooja saraah’s picture

StatusFileSize
new8.65 KB
new2.52 KB

Attached patch against #23

pooja saraah’s picture

StatusFileSize
new8.57 KB

Attached patch against #26

lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new1.96 KB
new8.24 KB

This should fix some of the fails and get rid of the unrelated changes.

lendude’s picture

Issue summary: View changes

Updated the IS a bit

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative

This doesn't apply cleanly to 10.1.x, so we'll likely need two patches.

There seems to be a fair bit of duplication between the three test methods, especially around the handler/relationship setup - perhaps we could refactor some of that into a helper method to make it easier to maintain, as we're already at the rule of three here.

But other than that, the code looks good to me.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

lendude’s picture

Status: Needs work » Needs review

Moved to MR and rerolled

smustgrave’s picture

Issue summary: View changes
Status: Needs review » Needs work

Hiding patches for clarity.

Started the issue summary update but not as familiar with the issue so left TBD in sections that I couldn't answer. Is the proposed solution correct? Steps to reproduce?

lendude’s picture

Issue summary: View changes
Status: Needs work » Needs review

Updated the IS

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks. Not sure because this is testing an infinite loop but when I tried to run the test-only feature it got hung up but did see an "E" in the output to show the failure.

So clearly test coverage is there haha.

Looking at the code change seems to appropriately catch the issue and throws the new exception.

Marking it but do new exceptions need a CR?

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I've added some comments to the MR.

lendude’s picture

Status: Needs work » Needs review

Addressed feedback. Not sure if the minor scope creep for php-stan is better than updating the base line and fixing it later, but it seems pretty minor so went for the fix

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Feedback around the return null appears to have been addressed.

quietone’s picture

Status: Reviewed & tested by the community » Needs work

I'm triaging RTBC issues. I read the IS and the comments and the MR. I didn't find any unanswered questions or other work to do.

However, I left one small suggestion for a comment in the MR. So setting back to NW for someone to check that suggestion.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.