Currently, in ViewsTest::testViewsAdmin() we add a relationship, but then don't use any of its fields. Which wouldn't work anyways, probably, since I think the test entities we create don't have any value for the "User ID" field.

So, we should, in this test method, add user IDs to the test entities (no need to re-index), then add some field from the relationship – e.g., the user roles – and see if those are displayed correctly, too.

Also, it might be interesting to do the same with an indexed field of a related entity – i.e., index the entity:entity_test/user_id:roles field and see if that gets displayed (and linked?) correctly, too. (Again, re-indexing is not necessary, since the data is loaded from entity storage anyways, at least with the DB backend we use for testing.)

Comments

drunken monkey created an issue. See original summary.

mikemiles86’s picture

mikemiles86’s picture

drunken monkey’s picture

Status: Active » Needs review

Finally got round to doing this, and it turned into a bit of a nightmare, but the attached should work fine and pass (at least when combined with #2700323-4: Views relationships not working – should probably merge these issues).

drunken monkey’s picture

Status: Needs review » Needs work

The last submitted patch, 5: 2657406-4--views_relationship_fields_tests.patch, failed testing.

The last submitted patch, 5: 2657406-4--views_relationship_fields_tests.patch, failed testing.

drunken monkey’s picture

Status: Needs work » Needs review
StatusFileSize
new13.64 KB
borisson_’s picture

Status: Needs review » Needs work

I'm glad that the tests agree with these changes and I like the additional coverage. It would be uncharacteristic to not find anything that could use slight improvements. Overall I think this patch is in great shape though, since I don't see any actual code that can be improved. I'm personally not a fan of the call_user_func_array call in the code, as that part doesn't read very well. However this is just moved around so not really something we should discuss/resolve in this particular issue.

  1. +++ b/src/Plugin/views/field/SearchApiEntity.php
    @@ -280,4 +283,21 @@ protected function getItem(EntityInterface $entity) {
    +   * @return string|null
    

    I think this should be string|NULL, however I see both used in core everywhere.

  2. +++ b/src/Plugin/views/field/SearchApiEntityField.php
    @@ -98,14 +98,46 @@ protected function getParentPath() {
    +    // The probably easiest way to do this is just use the $options we got from
    +    // the parent, but exclude those that come directly from the parent.
    

    This feels like a weird statement "The probably easiest way..."

    Can we replace that with: "We use the $options array from the parent, but exclude the items that come directly from the parent." or something similar?

  3. +++ b/src/Plugin/views/field/SearchApiFieldTrait.php
    @@ -390,23 +390,22 @@ public function preRender(&$values) {
    +            // by merging them together (currently it's an array of arrays, but
    +            // it should be just a flat array).
    

    Since we're touching this code anyway, let's add a @todo with a link to a d.o issue or remove the todo-part of the comment.

  4. +++ b/src/Plugin/views/field/SearchApiFieldTrait.php
    @@ -626,4 +625,14 @@ protected function getItemUrl(ResultRow $row, $i) {
    +   * Returns the Render API renderer.
    

    I can see this is something that's also described as "the render API renderer" in other places but this is the first time I've seen that. We can keep this though.

  5. +++ b/src/Tests/ViewsTest.php
    @@ -190,16 +190,31 @@ protected function checkResults(array $query, array $expected_results = NULL, $l
    +    // For viewing the user name and roles of the user associated with test
    +    // entities, the logged-in user needs to have the permission to administer
    +    // both users and permissions.
    

    That's a really awesome comment! @drunken monkey++

  6. +++ b/src/Tests/ViewsTest.php
    @@ -190,16 +190,31 @@ protected function checkResults(array $query, array $expected_results = NULL, $l
    +    // Set the user IDs associated with our test entities.
    

    I think this can benefit from a better description, how about: "To test the entity permissions, attach users with different permission sets to the entities"

  7. +++ b/src/Tests/ViewsTest.php
    @@ -253,7 +270,24 @@ public function testViewsAdmin() {
    +    // @todo For some strange reason, the "roles" field form is not included
    +    //   automatically in the series of field forms shown to us by Views. Deal
    +    //   with this graciously (since it's not really our fault, I hope), but it
    +    //   would be great to have this working normally.
    

    Should we resolve this in this issue? We probably should at least figure out if we're doing something wrong and adjust the comment accordingly. At the very least we can open a new issue for that.

  8. +++ b/src/Tests/ViewsTest.php
    @@ -301,10 +344,17 @@ public function testViewsAdmin() {
    +   * @return string|null
    

    See my other comment about this.

drunken monkey’s picture

Status: Needs work » Needs review
StatusFileSize
new953 bytes
new13.64 KB

I think this should be string|NULL, however I see both used in core everywhere.

No, it shouldn't.
You see both in Core because Core's relation to coding standards is that of a deer and Asimov's Laws – it might follow them most of the time, but more by accident than by concious acknowledgement.

Since we're touching this code anyway, let's add a @todo with a link to a d.o issue or remove the todo-part of the comment.

That's a really awesome comment! @drunken monkey++

That's just there because I spent over an hour tearing my hair out over why this isn't working, before figuring it out.
But nice that it at least lead to a good comment.

I think this can benefit from a better description, how about: "To test the entity permissions, attach users with different permission sets to the entities"

That's not what's happening, it's just about setting any users on the test entities. I just use those because they are already there, not because of their respective permissions. (And because using different users, with different names and roles, will probably make the test a bit less prone to false passes.)
A different suggestion for the comment text instead?

Should we resolve this in this issue? We probably should at least figure out if we're doing something wrong and adjust the comment accordingly. At the very least we can open a new issue for that.

I don't know, I have no idea what's going wrong there. You are free to try and debug it, though – I'd happily just ignore it, though. (Or create a follow-up issue, if you insist.)
Also, I remember the exact same thing happening when initially writing those Views field handler tests, there was also one field handler whose form wasn't (automatically) shown. But it seems that was resolved before I posted the first patch about it – I've got no idea how anymore, though.

  • drunken monkey committed 87b0af2 on 8.x-1.x
    Issue #2657406 by drunken monkey: Added tests for Views fields from...
drunken monkey’s picture

Status: Needs review » Fixed

Committed.
Thanks, everyone!

drunken monkey’s picture

Status: Fixed » Closed (fixed)

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