Issue #3364621 by lauriii, penyaskito, daffie, catch, phenaproxima, mondrake: Drupal\Tests\file\Kernel\Views\RelationshipNodeFileDataTest fails on HEAD with PostgreSQL
parent
0bde7162e5
commit
6e8cab8109
|
@ -79,6 +79,8 @@ class FileViewsData extends EntityViewsData {
|
|||
// Link ourselves to the {node_field_data} table
|
||||
// so we can provide node->file relationships.
|
||||
'node_field_data' => [
|
||||
'join_id' => 'casted_int_field_join',
|
||||
'cast' => 'right',
|
||||
'field' => 'id',
|
||||
'left_field' => 'nid',
|
||||
'extra' => [['field' => 'type', 'value' => 'node']],
|
||||
|
@ -86,6 +88,8 @@ class FileViewsData extends EntityViewsData {
|
|||
// Link ourselves to the {users_field_data} table
|
||||
// so we can provide user->file relationships.
|
||||
'users_field_data' => [
|
||||
'join_id' => 'casted_int_field_join',
|
||||
'cast' => 'right',
|
||||
'field' => 'id',
|
||||
'left_field' => 'uid',
|
||||
'extra' => [['field' => 'type', 'value' => 'user']],
|
||||
|
@ -93,6 +97,8 @@ class FileViewsData extends EntityViewsData {
|
|||
// Link ourselves to the {comment_field_data} table
|
||||
// so we can provide comment->file relationships.
|
||||
'comment' => [
|
||||
'join_id' => 'casted_int_field_join',
|
||||
'cast' => 'right',
|
||||
'field' => 'id',
|
||||
'left_field' => 'cid',
|
||||
'extra' => [['field' => 'type', 'value' => 'comment']],
|
||||
|
@ -100,6 +106,8 @@ class FileViewsData extends EntityViewsData {
|
|||
// Link ourselves to the {taxonomy_term_field_data} table
|
||||
// so we can provide taxonomy_term->file relationships.
|
||||
'taxonomy_term_data' => [
|
||||
'join_id' => 'casted_int_field_join',
|
||||
'cast' => 'right',
|
||||
'field' => 'id',
|
||||
'left_field' => 'tid',
|
||||
'extra' => [['field' => 'type', 'value' => 'taxonomy_term']],
|
||||
|
@ -122,6 +130,8 @@ class FileViewsData extends EntityViewsData {
|
|||
'real field' => 'id',
|
||||
'relationship' => [
|
||||
'id' => 'standard',
|
||||
'join_id' => 'casted_int_field_join',
|
||||
'cast' => 'left',
|
||||
'title' => $this->t('Content'),
|
||||
'label' => $this->t('Content'),
|
||||
'base' => 'node_field_data',
|
||||
|
@ -157,6 +167,8 @@ class FileViewsData extends EntityViewsData {
|
|||
'real field' => 'id',
|
||||
'relationship' => [
|
||||
'id' => 'standard',
|
||||
'join_id' => 'casted_int_field_join',
|
||||
'cast' => 'left',
|
||||
'title' => $this->t('User'),
|
||||
'label' => $this->t('User'),
|
||||
'base' => 'users',
|
||||
|
@ -174,6 +186,8 @@ class FileViewsData extends EntityViewsData {
|
|||
'real field' => 'fid',
|
||||
'relationship' => [
|
||||
'id' => 'standard',
|
||||
'join_id' => 'casted_int_field_join',
|
||||
'cast' => 'left',
|
||||
'title' => $this->t('File'),
|
||||
'label' => $this->t('File'),
|
||||
'base' => 'file_managed',
|
||||
|
@ -192,6 +206,8 @@ class FileViewsData extends EntityViewsData {
|
|||
'real field' => 'id',
|
||||
'relationship' => [
|
||||
'id' => 'standard',
|
||||
'join_id' => 'casted_int_field_join',
|
||||
'cast' => 'left',
|
||||
'title' => $this->t('Comment'),
|
||||
'label' => $this->t('Comment'),
|
||||
'base' => 'comment_field_data',
|
||||
|
@ -227,6 +243,8 @@ class FileViewsData extends EntityViewsData {
|
|||
'real field' => 'id',
|
||||
'relationship' => [
|
||||
'id' => 'standard',
|
||||
'join_id' => 'casted_int_field_join',
|
||||
'cast' => 'left',
|
||||
'title' => $this->t('Taxonomy Term'),
|
||||
'label' => $this->t('Taxonomy Term'),
|
||||
'base' => 'taxonomy_term_data',
|
||||
|
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace Drupal\Tests\file\Kernel\Views;
|
||||
|
||||
use Drupal\Core\Database\Database;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\file\Entity\File;
|
||||
use Drupal\file\FileInterface;
|
||||
|
@ -40,10 +39,6 @@ class RelationshipNodeFileDataTest extends ViewsKernelTestBase {
|
|||
protected function setUp($import_test_views = TRUE): void {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
if (Database::getConnection()->driver() === 'pgsql') {
|
||||
$this->markTestSkipped('Skipped because PostgreSQL is currently failing: https://www.drupal.org/project/drupal/issues/3364621');
|
||||
}
|
||||
|
||||
$this->installSchema('file', 'file_usage');
|
||||
$this->installEntitySchema('node');
|
||||
$this->installEntitySchema('file');
|
||||
|
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace Drupal\Tests\file\Kernel\Views;
|
||||
|
||||
use Drupal\Core\Database\Database;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\file\Entity\File;
|
||||
|
@ -51,10 +50,6 @@ class RelationshipUserFileDataTest extends ViewsKernelTestBase {
|
|||
protected function setUp($import_test_views = TRUE): void {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
if (Database::getConnection()->driver() === 'pgsql') {
|
||||
$this->markTestSkipped('Skipped because PostgreSQL is currently failing: https://www.drupal.org/project/drupal/issues/3364621');
|
||||
}
|
||||
|
||||
$this->installSchema('file', ['file_usage']);
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('file');
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
services:
|
||||
mysql.views.cast_sql:
|
||||
class: Drupal\mysql\Plugin\views\query\MysqlCastSql
|
||||
public: false
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\mysql\Plugin\views\query;
|
||||
|
||||
use Drupal\views\Plugin\views\query\CastSqlInterface;
|
||||
|
||||
/**
|
||||
* MySQL specific cast handling.
|
||||
*/
|
||||
class MysqlCastSql implements CastSqlInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldAsInt(string $field): string {
|
||||
return "CAST($field AS UNSIGNED)";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\mysql\Kernel\mysql\Plugin\views;
|
||||
|
||||
use Drupal\Tests\views\Kernel\Plugin\CastedIntFieldJoinTestBase;
|
||||
|
||||
/**
|
||||
* Tests MySQL specific cast handling.
|
||||
*
|
||||
* @group Database
|
||||
*/
|
||||
class MySqlCastedIntFieldJoinTest extends CastedIntFieldJoinTestBase {
|
||||
|
||||
/**
|
||||
* The db type that should be used for casting fields as integers.
|
||||
*/
|
||||
protected string $castingType = 'UNSIGNED';
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\pgsql\Kernel\pgsql\Plugin\views;
|
||||
|
||||
use Drupal\Tests\views\Kernel\Plugin\CastedIntFieldJoinTestBase;
|
||||
|
||||
/**
|
||||
* Tests PostgreSQL specific cast handling.
|
||||
*
|
||||
* @group Database
|
||||
*/
|
||||
class PgsqlCastedIntFieldJoinTest extends CastedIntFieldJoinTestBase {
|
||||
|
||||
/**
|
||||
* The db type that should be used for casting fields as integers.
|
||||
*/
|
||||
protected string $castingType = 'INTEGER';
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\sqlite\Kernel\sqlite\Plugin\views;
|
||||
|
||||
use Drupal\Tests\views\Kernel\Plugin\CastedIntFieldJoinTestBase;
|
||||
|
||||
/**
|
||||
* Tests SQLite specific cast handling.
|
||||
*
|
||||
* @group Database
|
||||
*/
|
||||
class SqliteCastedIntFieldJoinTest extends CastedIntFieldJoinTestBase {
|
||||
|
||||
/**
|
||||
* The db type that should be used for casting fields as integers.
|
||||
*/
|
||||
protected string $castingType = 'INTEGER';
|
||||
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\views\Plugin\views\join;
|
||||
|
||||
/**
|
||||
* Implementation for the "casted_int_field_join" join.
|
||||
*
|
||||
* This is needed for columns that are using a single table for tracking the
|
||||
* relationship to their items, but the referenced item IDs can be either
|
||||
* integers or strings and most DB engines (with MySQL as a notable exception)
|
||||
* are strict when comparing numbers and strings.
|
||||
*
|
||||
* @ingroup views_join_handlers
|
||||
*
|
||||
* @ViewsJoin("casted_int_field_join")
|
||||
*/
|
||||
class CastedIntFieldJoin extends JoinPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildJoin($select_query, $table, $view_query) {
|
||||
if (empty($this->configuration['table formula'])) {
|
||||
$right_table = $this->table;
|
||||
}
|
||||
else {
|
||||
$right_table = $this->configuration['table formula'];
|
||||
}
|
||||
|
||||
if ($this->leftTable) {
|
||||
$left_table = $view_query->getTableInfo($this->leftTable);
|
||||
$left_field = $this->leftFormula ?: "$left_table[alias].$this->leftField";
|
||||
}
|
||||
else {
|
||||
// This can be used if left_field is a formula or something. It should be
|
||||
// used only *very* rarely.
|
||||
$left_field = $this->leftField;
|
||||
$left_table = NULL;
|
||||
}
|
||||
|
||||
$right_field = "{$table['alias']}.$this->field";
|
||||
|
||||
assert(!isset($this->configuration['cast']) || in_array($this->configuration['cast'], ['right', 'left']));
|
||||
if (isset($this->configuration['cast']) && $this->configuration['cast'] === 'left') {
|
||||
$left_field = \Drupal::service('views.cast_sql')->getFieldAsInt($left_field);
|
||||
}
|
||||
else {
|
||||
$right_field = \Drupal::service('views.cast_sql')->getFieldAsInt($right_field);
|
||||
}
|
||||
|
||||
$condition = "$left_field {$this->configuration['operator']} $right_field";
|
||||
$arguments = [];
|
||||
|
||||
// Tack on the extra.
|
||||
if (isset($this->extra)) {
|
||||
$this->joinAddExtra($arguments, $condition, $table, $select_query, $left_table);
|
||||
}
|
||||
|
||||
$select_query->addJoin($this->type, $right_table, $table['alias'], $condition, $arguments);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\views\Plugin\views\query;
|
||||
|
||||
/**
|
||||
* Cast handling in SQL.
|
||||
*/
|
||||
class CastSql implements CastSqlInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldAsInt(string $field): string {
|
||||
return "CAST($field AS INTEGER)";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\views\Plugin\views\query;
|
||||
|
||||
/**
|
||||
* Defines an interface for defining cast expressions in SQL.
|
||||
*/
|
||||
interface CastSqlInterface {
|
||||
|
||||
/**
|
||||
* Returns a database expression to cast the field to int.
|
||||
*
|
||||
* @param $field string
|
||||
* The database field to cast.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFieldAsInt(string $field): string;
|
||||
|
||||
}
|
|
@ -0,0 +1,206 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\views\Kernel\Plugin;
|
||||
|
||||
use Drupal\Core\Database\Database;
|
||||
use Drupal\KernelTests\Core\Database\DriverSpecificKernelTestBase;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\views\Plugin\views\join\CastedIntFieldJoin;
|
||||
use Drupal\views\Tests\ViewTestData;
|
||||
use Drupal\views\Views;
|
||||
use Drupal\views\ViewExecutable;
|
||||
|
||||
/**
|
||||
* Tests the "casted_int_field_join" join plugin.
|
||||
*
|
||||
* @group views
|
||||
* @see \Drupal\views\Plugin\views\join\CastedIntFieldJoin
|
||||
*/
|
||||
abstract class CastedIntFieldJoinTestBase extends DriverSpecificKernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = [
|
||||
'system',
|
||||
'user',
|
||||
'views',
|
||||
'views_test_config',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $testViews = ['test_view'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $pluginId = 'casted_int_field_join';
|
||||
|
||||
/**
|
||||
* A plugin manager which handles the instances of joins.
|
||||
*
|
||||
* @var \Drupal\views\Plugin\ViewsPluginManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* @var \Drupal\user\Entity\User
|
||||
*/
|
||||
protected $rootUser;
|
||||
|
||||
/**
|
||||
* The db type that should be used for casting fields as integers.
|
||||
*
|
||||
* Needs to be overridden by the extending test for each specific engine.
|
||||
*/
|
||||
protected string $castingType;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->setUpFixtures();
|
||||
|
||||
ViewTestData::createTestViews(self::class, ['views_test_config']);
|
||||
|
||||
Views::viewsData()->clear();
|
||||
|
||||
// Add a join plugin manager which can be used in all of the tests.
|
||||
$this->manager = $this->container->get('plugin.manager.views.join');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUpFixtures() {
|
||||
$this->installEntitySchema('user');
|
||||
$this->installConfig(['user']);
|
||||
|
||||
// Create a record for uid 1.
|
||||
$this->rootUser = User::create(['name' => $this->randomMachineName()]);
|
||||
$this->rootUser->save();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public function testBuildJoin() {
|
||||
// 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]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a join using the given configuration.
|
||||
*
|
||||
* @param \Drupal\views\ViewExecutable $view
|
||||
* The view used in this test.
|
||||
* @param $configuration
|
||||
* The join plugin configuration.
|
||||
* @param $table_alias
|
||||
* The table alias to use for the join.
|
||||
*
|
||||
* @return array
|
||||
* The join information for the joined table. See
|
||||
* \Drupal\Core\Database\Query\Select::$tables for more information on the
|
||||
* structure of the array.
|
||||
*/
|
||||
protected function buildJoin(ViewExecutable $view, $configuration, $table_alias) {
|
||||
// Build the actual join values and read them back from the query object.
|
||||
$query = \Drupal::database()->select('node');
|
||||
|
||||
$join = $this->manager->createInstance($this->pluginId, $configuration);
|
||||
$this->assertInstanceOf(CastedIntFieldJoin::class, $join);
|
||||
|
||||
$table = ['alias' => $table_alias];
|
||||
$join->buildJoin($query, $table, $view->query);
|
||||
|
||||
$tables = $query->getTables();
|
||||
$join_info = $tables[$table['alias']];
|
||||
return $join_info;
|
||||
}
|
||||
|
||||
}
|
|
@ -100,3 +100,7 @@ services:
|
|||
class: Drupal\views\Plugin\views\query\SqliteDateSql
|
||||
arguments: ['@database']
|
||||
public: false
|
||||
views.cast_sql:
|
||||
class: Drupal\views\Plugin\views\query\CastSql
|
||||
tags:
|
||||
- { name: backend_overridable }
|
||||
|
|
Loading…
Reference in New Issue