diff --git a/core/misc/cspell/dictionary.txt b/core/misc/cspell/dictionary.txt index adaf63bb38..c9e0b9e9b6 100644 --- a/core/misc/cspell/dictionary.txt +++ b/core/misc/cspell/dictionary.txt @@ -577,7 +577,6 @@ johndoe jours jqueryui jsonapi -jsonb jsonlint jssdk jumplinks diff --git a/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php index 74452a444f..adfee76ac9 100644 --- a/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php +++ b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php @@ -468,7 +468,6 @@ public function getFieldTypeMap() { 'serial:normal' => 'serial', 'json:normal' => 'json', - 'json:binary' => 'jsonb', ]; return $map; } diff --git a/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php b/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php index 5a55217177..2c1c8d3566 100644 --- a/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php +++ b/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php @@ -244,67 +244,69 @@ public function testPgsqlExtensionExists(): void { } /** - * Tests json schema types. - * - * @param string $fieldType - * The Drupal field type name. - * @param string $insertData - * A JSON string to insert into the test_field column. - * @param string $selectField - * A select field to retrieve data. - * @param array $selectPlaceholder - * Placeholder values for the select field above. - * @param string $expected - * The expected value to assert. - * - * @dataProvider providerTestJsonSchema + * Tests json schema type. */ - public function testJsonSchema(string $fieldType, string $insertData, string $selectField, array $selectPlaceholder, string $expected): void { + public function testJsonSchema(): void { $table_specification = [ 'fields' => [ - 'id' => ['type' => 'serial', 'not null' => TRUE], - 'test_field' => ['type' => $fieldType], + 'id' => ['type' => 'serial', 'not null' => TRUE], + 'test_field' => ['type' => 'json'], ], 'primary key' => ['id'], ]; - $this->schema->createTable('test_json', $table_specification); + $this->assertTrue($this->schema->tableExists('test_json'), 'Table with database specific datatype was created.'); $this->connection->insert('test_json') - ->values(['test_field' => $insertData]) + ->fields([ + 'test_field' => '{"key": "value", "number": 0, "bool": true, "list": ["a","b","c"], "nested": {"key": "value"}}', + ]) ->execute(); - $query = $this->connection->select('test_json') - ->addExpression($selectField, NULL, $selectPlaceholder) - ->range(0, 1); - - $actual = $query->execute()->fetchField(0); - $this->assertSame($expected, $actual); - $this->assertTrue($this->schema->tableExists('test_json'), 'Table with database specific datatype was created.'); - } - - /** - * Provides test arguments for json schema tests. - * - * @return \string[][] - * The test arguments. - */ - public function providerTestJsonSchema() { - return [ - 'json:normal' => [ - 'json', - '{"key": "value", "number": 0, "bool": true, "list": ["a","b","c"], "nested": {"key": "value"}}', - "test_field->:key", - [':key' => 'key'], - 'value', - ], - 'json:binary' => [ - 'jsonb', - '{"key": "value", "number": 0, "bool": true, "list": ["a", "b", "c"], "nested": {"key": "value"}}', - "test_field->:nested->:key", - [':nested' => ':nested', ':key' => 'key'], - 'value', - ], - ]; + // The '->' operator returns strings wrapped into quotes. + $query = $this->connection->select('test_json'); + $query->addExpression('test_field -> :key', NULL, [ + ':key' => 'key', + ]); + $actual = $query->execute()->fetchField(); + $this->assertSame('"'. 'value' . '"', $actual); + // Testing nested strings using '->>' opearator. + $query = $this->connection->select('test_json'); + $query->addExpression('test_field -> :nested ->> :key', NULL, [ + ':nested' => 'nested', + ':key' => 'key', + ]); + $actual = $query->execute()->fetchField(); + $this->assertSame('value', $actual); + + // Testing boolean. + $query = $this->connection->select('test_json'); + $query->addExpression('test_field ->> :bool', NULL, [ + ':bool' => 'bool', + ]); + $actual = $query->execute()->fetchField(); + $this->assertSame('true', $actual); + // Testing numbers. + $query = $this->connection->select('test_json'); + $query->addExpression('test_field ->> :number', NULL, [ + ':number' => 'number', + ]); + $actual = $query->execute()->fetchField(); + $this->assertSame('0', $actual); + + // Testing array. + $query = $this->connection->select('test_json'); + $query->addExpression('test_field ->> :list', NULL, [ + ':list' => 'list', + ]); + $actual = $query->execute()->fetchField(); + $this->assertSame('["a","b","c"]', $actual); + // Testing array value. + $query = $this->connection->select('test_json'); + $query->addExpression('test_field -> :list ->> 1', NULL, [ + ':list' => 'list', + ]); + $actual = $query->execute()->fetchField(); + $this->assertSame('b', $actual); } }