function SqlTest::testLoadEntitiesWithRelationshipAndRevision

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

@covers ::loadEntities @covers ::assignEntitiesToResult

File

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

Class

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

Namespace

Drupal\Tests\views\Unit\Plugin\query

Code

public function testLoadEntitiesWithRelationshipAndRevision() : void {
    // 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__revision');
    $view_entity->get('base_field')
        ->willReturn('vid');
    $view->storage = $view_entity->reveal();
    $entities = [
        'second' => [
            11 => $this->prophesize(EntityInterface::class)
                ->reveal(),
            12 => $this->prophesize(EntityInterface::class)
                ->reveal(),
        ],
    ];
    $entity_revisions = [
        'first' => [
            1 => $this->prophesize(EntityInterface::class)
                ->reveal(),
            3 => $this->prophesize(EntityInterface::class)
                ->reveal(),
        ],
    ];
    $entity_type_manager = $this->setupEntityTypes($entities, $entity_revisions);
    $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([
        'vid' => 1,
        'entity_second__id' => 11,
    ]);
    // Provide an explicit NULL value, to test the case of a non required
    // relationship.
    $result[] = new ResultRow([
        'vid' => 1,
        'entity_second__id' => NULL,
    ]);
    $result[] = new ResultRow([
        'vid' => 3,
        'entity_second__id' => 12,
    ]);
    $query->addField('entity_first__revision', 'vid', 'vid');
    $query->addField('entity_second', 'id', 'entity_second__id');
    $query->loadEntities($result);
    $this->assertSame($entity_revisions['first'][1], $result[0]->_entity);
    $this->assertSame($entity_revisions['first'][1], $result[1]->_entity);
    $this->assertSame($entity_revisions['first'][3], $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.