You are here

public function JoinTest::testExamplePlugin in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Plugin/JoinTest.php \Drupal\Tests\views\Kernel\Plugin\JoinTest::testExamplePlugin()

Tests an example join plugin.

File

core/modules/views/tests/src/Kernel/Plugin/JoinTest.php, line 43

Class

JoinTest
Tests the join plugin.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testExamplePlugin() {

  // Setup a simple join and test the result sql.
  $view = Views::getView('test_view');
  $view
    ->initDisplay();
  $view
    ->initQuery();
  $configuration = [
    'left_table' => 'views_test_data',
    'left_field' => 'uid',
    'table' => 'users_field_data',
    'field' => 'uid',
  ];
  $join = $this->manager
    ->createInstance('join_test', $configuration);
  $this
    ->assertInstanceOf(JoinTestPlugin::class, $join);
  $rand_int = rand(0, 1000);
  $join
    ->setJoinValue($rand_int);
  $query = Database::getConnection()
    ->select('views_test_data');
  $table = [
    'alias' => 'users_field_data',
  ];
  $join
    ->buildJoin($query, $table, $view->query);
  $tables = $query
    ->getTables();
  $join_info = $tables['users_field_data'];
  $this
    ->assertStringContainsString("views_test_data.uid = {$rand_int}", $join_info['condition'], 'Make sure that the custom join plugin can extend the join base and alter the result.');
}