function SqlTest::testLoadEntitiesWithRelationship

Same name in other branches
  1. 8.9.x core/modules/views/tests/src/Unit/Plugin/query/SqlTest.php \Drupal\Tests\views\Unit\Plugin\query\SqlTest::testLoadEntitiesWithRelationship()
  2. 10 core/modules/views/tests/src/Unit/Plugin/query/SqlTest.php \Drupal\Tests\views\Unit\Plugin\query\SqlTest::testLoadEntitiesWithRelationship()
  3. 11.x core/modules/views/tests/src/Unit/Plugin/query/SqlTest.php \Drupal\Tests\views\Unit\Plugin\query\SqlTest::testLoadEntitiesWithRelationship()

@covers ::loadEntities @covers ::assignEntitiesToResult

File

core/modules/views/tests/src/Unit/Plugin/query/SqlTest.php, line 331

Class

SqlTest
@coversDefaultClass \Drupal\views\Plugin\views\query\Sql

Namespace

Drupal\Tests\views\Unit\Plugin\query

Code

public function testLoadEntitiesWithRelationship() {
    // We don't use prophecy, because prophecy enforces methods.
    $view = $this->getMockBuilder(ViewExecutable::class)
        ->disableOriginalConstructor()
        ->getMock();
    $this->setupViewWithRelationships($view);
    $view_entity = $this->prophesize(ViewEntityInterface::class);
    $view_entity->get('base_table')
        ->willReturn('entity_first');
    $view_entity->get('base_field')
        ->willReturn('id');
    $view->storage = $view_entity->reveal();
    $entities = [
        'first' => [
            1 => $this->prophesize(EntityInterface::class)
                ->reveal(),
            2 => $this->prophesize(EntityInterface::class)
                ->reveal(),
        ],
        'second' => [
            11 => $this->prophesize(EntityInterface::class)
                ->reveal(),
            12 => $this->prophesize(EntityInterface::class)
                ->reveal(),
        ],
    ];
    $entity_type_manager = $this->setupEntityTypes($entities);
    $date_sql = $this->prophesize(DateSqlInterface::class);
    $messenger = $this->prophesize(MessengerInterface::class);
    $query = new Sql([], 'sql', [], $entity_type_manager->reveal(), $date_sql->reveal(), $messenger->reveal());
    $query->view = $view;
    $result = [];
    $result[] = new ResultRow([
        'id' => 1,
        'entity_second__id' => 11,
    ]);
    // Provide an explicit NULL value, to test the case of a non required
    // relationship.
    $result[] = new ResultRow([
        'id' => 2,
        'entity_second__id' => NULL,
    ]);
    $result[] = new ResultRow([
        'id' => 2,
        'entity_second__id' => 12,
    ]);
    $query->addField('entity_first', 'id', 'id');
    $query->addField('entity_second', 'id', 'entity_second__id');
    $query->loadEntities($result);
    $this->assertSame($entities['first'][1], $result[0]->_entity);
    $this->assertSame($entities['first'][2], $result[1]->_entity);
    $this->assertSame($entities['first'][2], $result[2]->_entity);
    $this->assertSame($entities['second'][11], $result[0]->_relationship_entities['entity_second']);
    $this->assertEquals([], $result[1]->_relationship_entities);
    $this->assertSame($entities['second'][12], $result[2]->_relationship_entities['entity_second']);
}

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