This issue has novice tasks. If you are an experienced core developer and have multiple commit mentions, please review novices' work on these tasks rather than doing them yourself. Feedback from experienced contributors is valued.

Problem/Motivation

PHP warnings can be produced by the following line of code in views_preprocess_page() if $variables['attributes']['class'] is not set.

    $key = array_search('contextual-region', $variables['attributes']['class'] instanceof AttributeArray ? $variables['attributes']['class']->value() : $variables['attributes']['class']);

Steps to reproduce:

  1. Install minimal profile
  2. Enable views and views ui
  3. Go to /node whilst logged in as user 1

Proposed resolution

Fix code.

Remaining tasks

User interface changes

API changes

Comments

Stuart Miller’s picture

Assigned: Unassigned » Stuart Miller

I will take a look at this and see if I can produce an initial patch over this weekend.

Stuart Miller’s picture

Status: Active » Needs review
StatusFileSize
new810 bytes

Here is an initial patch file, I will try to add a test.

Views UI module is needs to be enabled in order to reproduce the warnings.

Stuart Miller’s picture

Status: Needs review » Needs work
Stuart Miller’s picture

Status: Needs work » Needs review
StatusFileSize
new799 bytes

Updating previous patch to change array_key_exists() for an isset() as array_key_exists() would return TRUE for a NULL value of the key.

harshil.maradiya’s picture

hi,
i am not able to reproduce this issue
i have followed following steps
Install minimal profile
Enable views
Go to /node whilst logged in as user 1
installed branch 8.x

please let me know i have missed any steps

Stuart Miller’s picture

Hello harshil.maradiya ,

The views_ui module needs to be enabled as well for the error to show.

alexpott’s picture

Issue summary: View changes
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you stuart!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Let's get a test written as this shouldn't be broken.

Stuart Miller’s picture

Status: Needs work » Needs review
StatusFileSize
new2.03 KB

Test added to patch

alexpott’s picture

@Stuart Miller it's be great if you can submit a test only patch so we can confirm that your test would catch the issue. Thanks!

Stuart Miller’s picture

@apexpott Here you go :)

I hope it's okay, I was unsure if I should be adding some kind of specific assert for the exceptions of if the system catching them is the expected/desired behaviour.

alexpott’s picture

+++ b/core/modules/views/lib/Drupal/views/Tests/FrontpageViewTest.php
@@ -0,0 +1,57 @@
+    $this->drupalGet('node');

It'd be great to have a comment as to why we doing this test. I.e. the views_ui adds contextual_links and the links are processed by views_preprocess_page() - or something like that.

Status: Needs review » Needs work

The last submitted patch, 12: 2191011-PHP-Warnings-in-views_preprocess_page-test-12.patch, failed testing.

Stuart Miller’s picture

Currently waiting on https://drupal.org/node/2194703 as test is to be added to class mentioned here.

Stuart Miller’s picture

Status: Needs work » Needs review
StatusFileSize
new969 bytes

Attached is patch file containing only the test for this.

Automated test will fail as it should to show that previous patch must be applied as well to correct the issue stated here.

Status: Needs review » Needs work

The last submitted patch, 16: 2191011-adding-admin-frontpage-view-test-16.patch, failed testing.

star-szr’s picture

Thanks @Stuart Miller! Since we have the fix as well it's good practice to post the test + fix (combined) patch as well in the same comment. Once we have that then the test-only patch usually only needs to be posted again if there are significant changes made to the test.

Here are some items that can be fixed up:

  1. +++ b/core/modules/node/lib/Drupal/node/Tests/Views/FrontPageTest.php
    @@ -149,4 +149,18 @@ protected function assertNotInResultSet(ViewExecutable $view, array $not_expecte
    +   * Tests the frontpage when logged in as admin
    

    This should be a complete sentence (end in a period) per https://drupal.org/node/1354#drupal.

  2. +++ b/core/modules/node/lib/Drupal/node/Tests/Views/FrontPageTest.php
    @@ -149,4 +149,18 @@ protected function assertNotInResultSet(ViewExecutable $view, array $not_expecte
    +  protected function testAdminFrontPage() {
    

    Should be public.

  3. +++ b/core/modules/node/lib/Drupal/node/Tests/Views/FrontPageTest.php
    @@ -149,4 +149,18 @@ protected function assertNotInResultSet(ViewExecutable $view, array $not_expecte
    +    // When user with sufficient permissions is logged in,
    +    // views_ui adds contextual links to the homepage view.
    +    // This verifies no errors are caused.
    

    The comments are wrapped really conservatively - they should be as long as possible within 80 characters (same reference as above).

Stuart Miller’s picture

Status: Needs work » Needs review
StatusFileSize
new1.72 KB

Thanks for you comments, I have amended the patch accordingly.

Both patch and test in same patch.

star-szr’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Looks great. Test passes and fails appropriately and fix looks good. Great work @Stuart Miller!

rajeev_drupal’s picture

Great work @Stuart. Fixes works fine for me as well!!

damiankloip’s picture

+++ b/core/modules/node/lib/Drupal/node/Tests/Views/FrontPageTest.php
@@ -149,4 +149,17 @@ protected function assertNotInResultSet(ViewExecutable $view, array $not_expecte
+    $this->drupalGet('node');

This makes sure no exceptions are thrown etc.. but I think we should assert that we can still see the view too? as you could have an empty body from that response and it would till pass.

So at the very least, a $this->assertResponse(200)?

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

@damiankloip good point! We definitely should be testing that the view is being rendered.

Stuart Miller’s picture

Status: Needs work » Needs review
StatusFileSize
new1.95 KB

I have added in an assert for the response code as well as one to check that the view appears on the page via a class name.

Thanks for pointing that out.

star-szr’s picture

Thanks! Please post an interdiff when updating patches :)

Minor docs/typo cleanup, leaving at 'Needs review' but these are easy fixes:

  1. +++ b/core/modules/node/lib/Drupal/node/Tests/Views/FrontPageTest.php
    @@ -149,4 +149,21 @@ protected function assertNotInResultSet(ViewExecutable $view, array $not_expecte
    +    // When user with sufficient permissions is logged in, views_ui adds
    +    // contextual links to the homepage view. This verifies there are no errors.
    

    This is a bit terse, how about "When a user"?

  2. +++ b/core/modules/node/lib/Drupal/node/Tests/Views/FrontPageTest.php
    @@ -149,4 +149,21 @@ protected function assertNotInResultSet(ViewExecutable $view, array $not_expecte
    +    // Check that correct response was recieved from last request.
    

    Again a bit terse and there's a typo on 'received'. How about something like:

    Check that the correct response was received from the last request.

    Having said that, I'd also suggest that we don't need to comment every single line of this test method. I think this one in particular could definitely do without a comment :)

  3. +++ b/core/modules/node/lib/Drupal/node/Tests/Views/FrontPageTest.php
    @@ -149,4 +149,21 @@ protected function assertNotInResultSet(ViewExecutable $view, array $not_expecte
    +    // Check that fontpage view was rendered.
    

    fontpage should be frontpage, and similar to the above comments maybe we can add a 'the' between Check and frontpage :)

Stuart Miller’s picture

Sorry about those typos, that's pretty sloppy!

I have amended and this time attached an interdiff from the previous patch #19

Thanks

star-szr’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @Stuart Miller, that looks good to me.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

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