function CastedIntFieldJoinTestBase::testBuildJoin

Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Kernel/Plugin/CastedIntFieldJoinTestBase.php \Drupal\Tests\views\Kernel\Plugin\CastedIntFieldJoinTestBase::testBuildJoin()

Tests base join functionality.

This duplicates parts of \Drupal\Tests\views\Kernel\Plugin\JoinTest::testBasePlugin() to ensure that no functionality provided by the base join plugin is broken.

File

core/modules/views/tests/src/Kernel/Plugin/CastedIntFieldJoinTestBase.php, line 97

Class

CastedIntFieldJoinTestBase
Tests the "casted_int_field_join" join plugin.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testBuildJoin() : void {
  // Setup a simple join and test the result sql.
  $view = Views::getView('test_view');
  $view->initDisplay();
  $view->initQuery();
  $connection = Database::getConnection();
  // First define a simple join without an extra condition.
  // Set the various options on the join object.
  $configuration = [
    'left_table' => 'views_test_data',
    'left_field' => 'uid',
    'table' => 'users_field_data',
    'field' => 'uid',
    'adjusted' => TRUE,
  ];
  $join = $this->manager
    ->createInstance($this->pluginId, $configuration);
  $this->assertInstanceOf(CastedIntFieldJoin::class, $join);
  $this->assertNull($join->extra);
  $this->assertTrue($join->adjusted);
  $join_info = $this->buildJoin($view, $configuration, 'users_field_data');
  $this->assertSame($join_info['join type'], 'LEFT');
  $this->assertSame($join_info['table'], $configuration['table']);
  $this->assertSame($join_info['alias'], 'users_field_data');
  $this->assertSame($join_info['condition'], "views_test_data.uid = CAST(users_field_data.uid AS {$this->castingType})");
  // Set a different alias and make sure table info is as expected.
  $join_info = $this->buildJoin($view, $configuration, 'users1');
  $this->assertSame($join_info['alias'], 'users1');
  // Set a different join type (INNER) and make sure it is used.
  $configuration['type'] = 'INNER';
  $join_info = $this->buildJoin($view, $configuration, 'users2');
  $this->assertSame($join_info['join type'], 'INNER');
  // Setup addition conditions and make sure it is used.
  $random_name_1 = $this->randomMachineName();
  $random_name_2 = $this->randomMachineName();
  $configuration['cast'] = 'left';
  $configuration['extra'] = [
    [
      'field' => 'name',
      'value' => $random_name_1,
    ],
    [
      'field' => 'name',
      'value' => $random_name_2,
      'operator' => '<>',
    ],
  ];
  $join_info = $this->buildJoin($view, $configuration, 'users3');
  $this->assertStringContainsString("CAST(views_test_data.uid AS {$this->castingType}) = users3.uid", $join_info['condition']);
  $this->assertStringContainsString('users3.name = :views_join_condition_0', $join_info['condition']);
  $this->assertStringContainsString('users3.name <> :views_join_condition_1', $join_info['condition']);
  $this->assertSame(array_values($join_info['arguments']), [
    $random_name_1,
    $random_name_2,
  ]);
  // Test that 'IN' conditions are properly built.
  $random_name_1 = $this->randomMachineName();
  $random_name_2 = $this->randomMachineName();
  $random_name_3 = $this->randomMachineName();
  $random_name_4 = $this->randomMachineName();
  $configuration['cast'] = 'right';
  $configuration['extra'] = [
    [
      'field' => 'name',
      'value' => $random_name_1,
    ],
    [
      'field' => 'name',
      'value' => [
        $random_name_2,
        $random_name_3,
        $random_name_4,
      ],
    ],
  ];
  $join_info = $this->buildJoin($view, $configuration, 'users4');
  $this->assertStringContainsString("views_test_data.uid = CAST(users4.uid AS {$this->castingType})", $join_info['condition']);
  $this->assertStringContainsString('users4.name = :views_join_condition_0', $join_info['condition']);
  $this->assertStringContainsString('users4.name IN ( :views_join_condition_1[] )', $join_info['condition']);
  $this->assertSame($join_info['arguments'][':views_join_condition_1[]'], [
    $random_name_2,
    $random_name_3,
    $random_name_4,
  ]);
}

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