function EntityReferenceRelationshipTest::testGroupByWithEmptyRelationships

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Kernel/EntityReference/Views/EntityReferenceRelationshipTest.php \Drupal\Tests\field\Kernel\EntityReference\Views\EntityReferenceRelationshipTest::testGroupByWithEmptyRelationships()
  2. 8.9.x core/modules/field/tests/src/Kernel/EntityReference/Views/EntityReferenceRelationshipTest.php \Drupal\Tests\field\Kernel\EntityReference\Views\EntityReferenceRelationshipTest::testGroupByWithEmptyRelationships()
  3. 10 core/modules/field/tests/src/Kernel/EntityReference/Views/EntityReferenceRelationshipTest.php \Drupal\Tests\field\Kernel\EntityReference\Views\EntityReferenceRelationshipTest::testGroupByWithEmptyRelationships()

Tests group by with optional and empty relationship.

File

core/modules/field/tests/src/Kernel/EntityReference/Views/EntityReferenceRelationshipTest.php, line 296

Class

EntityReferenceRelationshipTest
Tests entity reference relationship data.

Namespace

Drupal\Tests\field\Kernel\EntityReference\Views

Code

public function testGroupByWithEmptyRelationships() : void {
    $entities = [];
    // Create 4 entities with name1 and 3 entities with name2.
    for ($i = 1; $i <= 4; $i++) {
        $entity = [
            'name' => 'name' . $i,
        ];
        $entity = EntityTest::create($entity);
        $entities[] = $entity;
        $entity->save();
    }
    $entity = EntityTestMul::create([
        'name' => 'name1',
    ]);
    $entity->field_data_test_unlimited = [
        [
            'target_id' => $entities[0]->id(),
        ],
        [
            'target_id' => $entities[1]->id(),
        ],
        [
            'target_id' => $entities[2]->id(),
        ],
    ];
    $entity->save();
    $entity = EntityTestMul::create([
        'name' => 'name2',
    ]);
    $entity->field_data_test_unlimited = [
        [
            'target_id' => $entities[0]->id(),
        ],
        [
            'target_id' => $entities[1]->id(),
        ],
    ];
    $entity->save();
    $entity = EntityTestMul::create([
        'name' => 'name3',
    ]);
    $entity->field_data_test_unlimited->target_id = $entities[0]->id();
    $entity->save();
    $view = Views::getView('test_entity_reference_group_by_empty_relationships');
    $this->executeView($view);
    $this->assertCount(4, $view->result);
    // First three results should contain a reference from EntityTestMul.
    $this->assertNotEmpty($view->getStyle()
        ->getField(0, 'name_2'));
    $this->assertNotEmpty($view->getStyle()
        ->getField(1, 'name_2'));
    $this->assertNotEmpty($view->getStyle()
        ->getField(2, 'name_2'));
    // Fourth result has no reference from EntityTestMul hence the output for
    // should be empty.
    $this->assertEquals('', $view->getStyle()
        ->getField(3, 'name_2'));
    $fields = $view->field;
    // Check getValue for reference with a value. The first 3 rows reference
    // EntityTestMul, so have value 'name1'.
    $this->assertEquals('name1', $fields['name_2']->getValue($view->result[0]));
    $this->assertEquals('name1', $fields['name_2']->getValue($view->result[1]));
    $this->assertEquals('name1', $fields['name_2']->getValue($view->result[2]));
    // Ensure getValue works on empty references.
    $this->assertNull($fields['name_2']->getValue($view->result[3]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.