updated core to 8.6.1 via composer
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Drupal\KernelTests\Component\Utility;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
@@ -28,7 +28,7 @@ class SafeMarkupKernelTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets arguments for SafeMarkup::format() based on Url::fromUri() parameters.
|
||||
* Gets arguments for FormattableMarkup based on Url::fromUri() parameters.
|
||||
*
|
||||
* @param string $uri
|
||||
* The URI of the resource.
|
||||
@@ -38,6 +38,8 @@ class SafeMarkupKernelTest extends KernelTestBase {
|
||||
* @return array
|
||||
* Array containing:
|
||||
* - ':url': A URL string.
|
||||
*
|
||||
* @see \Drupal\Component\Render\FormattableMarkup
|
||||
*/
|
||||
protected static function getSafeMarkupUriArgs($uri, $options = []) {
|
||||
$args[':url'] = Url::fromUri($uri, $options)->toString();
|
||||
@@ -45,13 +47,13 @@ class SafeMarkupKernelTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests URL ":placeholders" in SafeMarkup::format().
|
||||
* Tests URL ":placeholders" in \Drupal\Component\Render\FormattableMarkup.
|
||||
*
|
||||
* @dataProvider providerTestSafeMarkupUri
|
||||
*/
|
||||
public function testSafeMarkupUri($string, $uri, $options, $expected) {
|
||||
$args = self::getSafeMarkupUriArgs($uri, $options);
|
||||
$this->assertEquals($expected, SafeMarkup::format($string, $args));
|
||||
$this->assertEquals($expected, new FormattableMarkup($string, $args));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,7 +115,7 @@ class SafeMarkupKernelTest extends KernelTestBase {
|
||||
$this->setExpectedException(\InvalidArgumentException::class);
|
||||
$args = self::getSafeMarkupUriArgs($uri);
|
||||
|
||||
SafeMarkup::format($string, $args);
|
||||
new FormattableMarkup($string, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -155,11 +155,12 @@ class TypedConfigTest extends KernelTestBase {
|
||||
$value = $typed_config->getValue();
|
||||
unset($value['giraffe']);
|
||||
$value['elephant'] = 'foo';
|
||||
$value['zebra'] = 'foo';
|
||||
$typed_config->setValue($value);
|
||||
$result = $typed_config->validate();
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertEquals('', $result->get(0)->getPropertyPath());
|
||||
$this->assertEquals('Missing giraffe.', $result->get(0)->getMessage());
|
||||
$this->assertEquals('Unexpected keys: elephant, zebra', $result->get(0)->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\KernelTests;
|
||||
|
||||
use Drupal\Core\Form\FormState;
|
||||
|
||||
/**
|
||||
* Full generic test suite for any form that data with the configuration system.
|
||||
*
|
||||
* @see UserAdminSettingsFormTest
|
||||
* For a full working implementation.
|
||||
*/
|
||||
abstract class ConfigFormTestBase extends KernelTestBase {
|
||||
/**
|
||||
* Form ID to use for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Form\FormInterface
|
||||
*/
|
||||
protected $form;
|
||||
|
||||
/**
|
||||
* Values to use for testing.
|
||||
*
|
||||
* Contains details for form key, configuration object name, and config key.
|
||||
* Example:
|
||||
* @code
|
||||
* array(
|
||||
* 'user_mail_cancel_confirm_body' => array(
|
||||
* '#value' => $this->randomString(),
|
||||
* '#config_name' => 'user.mail',
|
||||
* '#config_key' => 'cancel_confirm.body',
|
||||
* ),
|
||||
* );
|
||||
* @endcode
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $values;
|
||||
|
||||
/**
|
||||
* Submit the system_config_form ensure the configuration has expected values.
|
||||
*/
|
||||
public function testConfigForm() {
|
||||
// Programmatically submit the given values.
|
||||
$values = [];
|
||||
foreach ($this->values as $form_key => $data) {
|
||||
$values[$form_key] = $data['#value'];
|
||||
}
|
||||
$form_state = (new FormState())->setValues($values);
|
||||
\Drupal::formBuilder()->submitForm($this->form, $form_state);
|
||||
|
||||
// Check that the form returns an error when expected, and vice versa.
|
||||
$errors = $form_state->getErrors();
|
||||
$valid_form = empty($errors);
|
||||
$args = [
|
||||
'%values' => print_r($values, TRUE),
|
||||
'%errors' => $valid_form ? t('None') : implode(' ', $errors),
|
||||
];
|
||||
$this->assertTrue($valid_form, format_string('Input values: %values<br/>Validation handler errors: %errors', $args));
|
||||
|
||||
foreach ($this->values as $data) {
|
||||
$this->assertEqual($data['#value'], $this->config($data['#config_name'])->get($data['#config_key']));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Action;
|
||||
|
||||
use Drupal\Core\Action\Plugin\Action\Derivative\EntityDeleteActionDeriver;
|
||||
use Drupal\entity_test\Entity\EntityTestMulRevPub;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\system\Entity\Action;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
/**
|
||||
* @group Action
|
||||
*/
|
||||
class DeleteActionTest extends KernelTestBase {
|
||||
|
||||
protected $testUser;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['system', 'entity_test', 'user'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('entity_test_mulrevpub');
|
||||
$this->installEntitySchema('user');
|
||||
$this->installSchema('system', ['sequences', 'key_value_expire']);
|
||||
|
||||
$this->testUser = User::create([
|
||||
'name' => 'foobar',
|
||||
'mail' => 'foobar@example.com',
|
||||
]);
|
||||
$this->testUser->save();
|
||||
\Drupal::service('current_user')->setAccount($this->testUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Drupal\Core\Action\Plugin\Action\Derivative\EntityDeleteActionDeriver::getDerivativeDefinitions
|
||||
*/
|
||||
public function testGetDerivativeDefinitions() {
|
||||
$deriver = new EntityDeleteActionDeriver(\Drupal::entityTypeManager(), \Drupal::translation());
|
||||
$this->assertEquals([
|
||||
'entity_test_mulrevpub' => [
|
||||
'type' => 'entity_test_mulrevpub',
|
||||
'label' => 'Delete test entity - revisions, data table, and published interface',
|
||||
'action_label' => 'Delete',
|
||||
'confirm_form_route_name' => 'entity.entity_test_mulrevpub.delete_multiple_form',
|
||||
],
|
||||
'entity_test_rev' => [
|
||||
'type' => 'entity_test_rev',
|
||||
'label' => 'Delete test entity - revisions',
|
||||
'action_label' => 'Delete',
|
||||
'confirm_form_route_name' => 'entity.entity_test_rev.delete_multiple_form',
|
||||
],
|
||||
], $deriver->getDerivativeDefinitions([
|
||||
'action_label' => 'Delete',
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Drupal\Core\Action\Plugin\Action\DeleteAction::execute
|
||||
*/
|
||||
public function testDeleteAction() {
|
||||
$entity = EntityTestMulRevPub::create(['name' => 'test']);
|
||||
$entity->save();
|
||||
|
||||
$action = Action::create([
|
||||
'id' => 'entity_delete_action',
|
||||
'plugin' => 'entity:delete_action:entity_test_mulrevpub',
|
||||
]);
|
||||
$action->save();
|
||||
|
||||
$action->execute([$entity]);
|
||||
$this->assertArraySubset(['module' => ['entity_test']], $action->getDependencies());
|
||||
|
||||
/** @var \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store */
|
||||
$temp_store = \Drupal::service('tempstore.private');
|
||||
$store_entries = $temp_store->get('entity_delete_multiple_confirm')->get($this->testUser->id() . ':entity_test_mulrevpub');
|
||||
$this->assertArraySubset([$this->testUser->id() => ['en' => 'en']], $store_entries);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,7 +29,7 @@ class PublishActionTest extends KernelTestBase {
|
||||
* @covers \Drupal\Core\Action\Plugin\Action\Derivative\EntityPublishedActionDeriver::getDerivativeDefinitions
|
||||
*/
|
||||
public function testGetDerivativeDefinitions() {
|
||||
$deriver = new EntityPublishedActionDeriver(\Drupal::entityTypeManager());
|
||||
$deriver = new EntityPublishedActionDeriver(\Drupal::entityTypeManager(), \Drupal::translation());
|
||||
$this->assertArraySubset([
|
||||
'entity_test_mulrevpub' => [
|
||||
'type' => 'entity_test_mulrevpub',
|
||||
|
||||
@@ -29,7 +29,7 @@ class SaveActionTest extends KernelTestBase {
|
||||
* @covers \Drupal\Core\Action\Plugin\Action\Derivative\EntityChangedActionDeriver::getDerivativeDefinitions
|
||||
*/
|
||||
public function testGetDerivativeDefinitions() {
|
||||
$deriver = new EntityChangedActionDeriver(\Drupal::entityTypeManager());
|
||||
$deriver = new EntityChangedActionDeriver(\Drupal::entityTypeManager(), \Drupal::translation());
|
||||
$this->assertArraySubset([
|
||||
'entity_test_mul_changed' => [
|
||||
'type' => 'entity_test_mul_changed',
|
||||
|
||||
@@ -3,14 +3,13 @@
|
||||
namespace Drupal\KernelTests\Core\Asset;
|
||||
|
||||
use Drupal\Component\Serialization\Json;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Asset\AttachedAssets;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests #attached assets: attached asset libraries and JavaScript settings.
|
||||
*
|
||||
* i.e. tests:
|
||||
* I.e. tests:
|
||||
*
|
||||
* @code
|
||||
* $build['#attached']['library'] = …
|
||||
@@ -205,7 +204,7 @@ class AttachedAssetsTest extends KernelTestBase {
|
||||
$end = strrpos($rendered_js, $endToken);
|
||||
// Convert to a string, as $renderer_js is a \Drupal\Core\Render\Markup
|
||||
// object.
|
||||
$json = Unicode::substr($rendered_js, $start, $end - $start + 1);
|
||||
$json = mb_substr($rendered_js, $start, $end - $start + 1);
|
||||
$parsed_settings = Json::decode($json);
|
||||
|
||||
// Test whether the settings for core/drupalSettings are available.
|
||||
|
||||
+2
-4
@@ -149,7 +149,6 @@ class ResolvedLibraryDefinitionsFilesMatchTest extends KernelTestBase {
|
||||
* so on.
|
||||
*/
|
||||
protected function verifyLibraryFilesExist($library_definitions) {
|
||||
$root = \Drupal::root();
|
||||
foreach ($library_definitions as $extension => $libraries) {
|
||||
foreach ($libraries as $library_name => $library) {
|
||||
if (in_array("$extension/$library_name", $this->librariesToSkip)) {
|
||||
@@ -160,7 +159,7 @@ class ResolvedLibraryDefinitionsFilesMatchTest extends KernelTestBase {
|
||||
foreach (['css', 'js'] as $asset_type) {
|
||||
foreach ($library[$asset_type] as $asset) {
|
||||
$file = $asset['data'];
|
||||
$path = $root . '/' . $file;
|
||||
$path = $this->root . '/' . $file;
|
||||
// Only check and assert each file path once.
|
||||
if (!isset($this->pathsChecked[$path])) {
|
||||
$this->assertTrue(is_file($path), "$file file referenced from the $extension/$library_name library exists.");
|
||||
@@ -192,10 +191,9 @@ class ResolvedLibraryDefinitionsFilesMatchTest extends KernelTestBase {
|
||||
|
||||
$libraries['core'] = $this->libraryDiscovery->getLibrariesByExtension('core');
|
||||
|
||||
$root = \Drupal::root();
|
||||
foreach ($extensions as $extension_name => $extension) {
|
||||
$library_file = $extension->getPath() . '/' . $extension_name . '.libraries.yml';
|
||||
if (is_file($root . '/' . $library_file)) {
|
||||
if (is_file($this->root . '/' . $library_file)) {
|
||||
$libraries[$extension_name] = $this->libraryDiscovery->getLibrariesByExtension($extension_name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,20 +49,24 @@ class GetFilenameTest extends KernelTestBase {
|
||||
// a fixed location and naming.
|
||||
$this->assertIdentical(drupal_get_filename('profile', 'testing'), 'core/profiles/testing/testing.info.yml');
|
||||
|
||||
// Generate a non-existing module name.
|
||||
$non_existing_module = uniqid("", TRUE);
|
||||
|
||||
// Set a custom error handler so we can ignore the file not found error.
|
||||
set_error_handler(function ($severity, $message, $file, $line) {
|
||||
// Skip error handling if this is a "file not found" error.
|
||||
if (strstr($message, 'is missing from the file system:')) {
|
||||
\Drupal::state()->set('get_filename_test_triggered_error', TRUE);
|
||||
\Drupal::state()->set('get_filename_test_triggered_error', $message);
|
||||
return;
|
||||
}
|
||||
throw new \ErrorException($message, 0, $severity, $file, $line);
|
||||
});
|
||||
$this->assertNull(drupal_get_filename('module', $non_existing_module), 'Searching for an item that does not exist returns NULL.');
|
||||
$this->assertTrue(\Drupal::state()->get('get_filename_test_triggered_error'), 'Searching for an item that does not exist triggers an error.');
|
||||
$this->assertNull(drupal_get_filename('module', 'there_is_a_module_for_that'), 'Searching for an item that does not exist returns NULL.');
|
||||
$this->assertEquals('The following module is missing from the file system: there_is_a_module_for_that', \Drupal::state()->get('get_filename_test_triggered_error'));
|
||||
|
||||
$this->assertNull(drupal_get_filename('theme', 'there_is_a_theme_for_you'), 'Searching for an item that does not exist returns NULL.');
|
||||
$this->assertEquals('The following theme is missing from the file system: there_is_a_theme_for_you', \Drupal::state()->get('get_filename_test_triggered_error'));
|
||||
|
||||
$this->assertNull(drupal_get_filename('profile', 'there_is_an_install_profile_for_you'), 'Searching for an item that does not exist returns NULL.');
|
||||
$this->assertEquals('The following profile is missing from the file system: there_is_an_install_profile_for_you', \Drupal::state()->get('get_filename_test_triggered_error'));
|
||||
|
||||
// Restore the original error handler.
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Command;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Core\Command\DbDumpApplication;
|
||||
use Drupal\Core\Config\DatabaseStorage;
|
||||
use Drupal\Core\Database\Database;
|
||||
@@ -205,9 +205,9 @@ class DbDumpTest extends KernelTestBase {
|
||||
foreach ($this->tables as $table) {
|
||||
$this->assertTrue(Database::getConnection()
|
||||
->schema()
|
||||
->tableExists($table), SafeMarkup::format('Table @table created by the database script.', ['@table' => $table]));
|
||||
$this->assertSame($this->originalTableSchemas[$table], $this->getTableSchema($table), SafeMarkup::format('The schema for @table was properly restored.', ['@table' => $table]));
|
||||
$this->assertSame($this->originalTableIndexes[$table], $this->getTableIndexes($table), SafeMarkup::format('The indexes for @table were properly restored.', ['@table' => $table]));
|
||||
->tableExists($table), new FormattableMarkup('Table @table created by the database script.', ['@table' => $table]));
|
||||
$this->assertSame($this->originalTableSchemas[$table], $this->getTableSchema($table), new FormattableMarkup('The schema for @table was properly restored.', ['@table' => $table]));
|
||||
$this->assertSame($this->originalTableIndexes[$table], $this->getTableIndexes($table), new FormattableMarkup('The indexes for @table were properly restored.', ['@table' => $table]));
|
||||
}
|
||||
|
||||
// Ensure the test config has been replaced.
|
||||
|
||||
@@ -6,12 +6,16 @@ use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* @covers ::drupal_set_message
|
||||
* @group PHPUnit
|
||||
* @group Common
|
||||
* @group legacy
|
||||
*/
|
||||
class DrupalSetMessageTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* The basic functionality of drupal_set_message().
|
||||
*
|
||||
* @expectedDeprecation drupal_set_message() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. See https://www.drupal.org/node/2774931
|
||||
* @expectedDeprecation drupal_get_message() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::all() or \Drupal\Core\Messenger\MessengerInterface::messagesByType() instead. See https://www.drupal.org/node/2774931
|
||||
*/
|
||||
public function testDrupalSetMessage() {
|
||||
drupal_set_message(t('A message: @foo', ['@foo' => 'bar']));
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ class CacheabilityMetadataConfigOverrideTest extends KernelTestBase {
|
||||
'config',
|
||||
'config_override_test',
|
||||
'system',
|
||||
'user'
|
||||
'user',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Drupal\KernelTests\Core\Config;
|
||||
|
||||
use Drupal\Component\Utility\Crypt;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Core\Config\ConfigNameException;
|
||||
use Drupal\Core\Config\ConfigValueException;
|
||||
use Drupal\Core\Config\InstallStorage;
|
||||
@@ -292,7 +292,7 @@ class ConfigCRUDTest extends KernelTestBase {
|
||||
$this->fail('No Exception thrown upon saving invalid data type.');
|
||||
}
|
||||
catch (UnsupportedDataTypeConfigException $e) {
|
||||
$this->pass(SafeMarkup::format('%class thrown upon saving invalid data type.', [
|
||||
$this->pass(new FormattableMarkup('%class thrown upon saving invalid data type.', [
|
||||
'%class' => get_class($e),
|
||||
]));
|
||||
}
|
||||
@@ -309,7 +309,7 @@ class ConfigCRUDTest extends KernelTestBase {
|
||||
$this->fail('No Exception thrown upon saving invalid data type.');
|
||||
}
|
||||
catch (UnsupportedDataTypeConfigException $e) {
|
||||
$this->pass(SafeMarkup::format('%class thrown upon saving invalid data type.', [
|
||||
$this->pass(new FormattableMarkup('%class thrown upon saving invalid data type.', [
|
||||
'%class' => get_class($e),
|
||||
]));
|
||||
}
|
||||
|
||||
@@ -50,9 +50,9 @@ class ConfigDependencyTest extends EntityKernelTestBase {
|
||||
'id' => 'entity1',
|
||||
'dependencies' => [
|
||||
'enforced' => [
|
||||
'module' => ['node']
|
||||
]
|
||||
]
|
||||
'module' => ['node'],
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
$entity1->save();
|
||||
@@ -200,7 +200,7 @@ class ConfigDependencyTest extends EntityKernelTestBase {
|
||||
'id' => 'entity1',
|
||||
'dependencies' => [
|
||||
'enforced' => [
|
||||
'module' => ['node', 'config_test']
|
||||
'module' => ['node', 'config_test'],
|
||||
],
|
||||
],
|
||||
]
|
||||
@@ -264,7 +264,7 @@ class ConfigDependencyTest extends EntityKernelTestBase {
|
||||
'id' => 'entity_' . $entity_id_suffixes[0],
|
||||
'dependencies' => [
|
||||
'enforced' => [
|
||||
'module' => ['node', 'config_test']
|
||||
'module' => ['node', 'config_test'],
|
||||
],
|
||||
],
|
||||
]
|
||||
@@ -309,7 +309,7 @@ class ConfigDependencyTest extends EntityKernelTestBase {
|
||||
'dependencies' => [
|
||||
'enforced' => [
|
||||
'config' => [$entity_1->getConfigDependencyName()],
|
||||
'module' => ['node', 'config_test']
|
||||
'module' => ['node', 'config_test'],
|
||||
],
|
||||
],
|
||||
]
|
||||
@@ -508,7 +508,7 @@ class ConfigDependencyTest extends EntityKernelTestBase {
|
||||
// Test dependencies between configuration entities.
|
||||
$entity1 = $storage->create(
|
||||
[
|
||||
'id' => 'entity1'
|
||||
'id' => 'entity1',
|
||||
]
|
||||
);
|
||||
$entity1->save();
|
||||
@@ -621,7 +621,7 @@ class ConfigDependencyTest extends EntityKernelTestBase {
|
||||
'id' => 'entity1',
|
||||
'dependencies' => [
|
||||
'enforced' => [
|
||||
'content' => [$content_entity->getConfigDependencyName()]
|
||||
'content' => [$content_entity->getConfigDependencyName()],
|
||||
],
|
||||
],
|
||||
]
|
||||
@@ -632,7 +632,7 @@ class ConfigDependencyTest extends EntityKernelTestBase {
|
||||
'id' => 'entity2',
|
||||
'dependencies' => [
|
||||
'enforced' => [
|
||||
'config' => [$entity1->getConfigDependencyName()]
|
||||
'config' => [$entity1->getConfigDependencyName()],
|
||||
],
|
||||
],
|
||||
]
|
||||
|
||||
@@ -31,7 +31,7 @@ class ConfigEntityNormalizeTest extends KernelTestBase {
|
||||
$config = $this->config('config_test.dynamic.system');
|
||||
$data = [
|
||||
'label' => 'foobar',
|
||||
'additional_key' => TRUE
|
||||
'additional_key' => TRUE,
|
||||
] + $config->getRawData();
|
||||
$config->setData($data)->save();
|
||||
$this->assertNotIdentical($config_entity->toArray(), $config->getRawData(), 'Stored config entity is not is equivalent to config schema.');
|
||||
|
||||
@@ -97,7 +97,7 @@ class ConfigEntityUnitTest extends KernelTestBase {
|
||||
$entity = $this->storage->create([
|
||||
'id' => $this->randomMachineName(),
|
||||
'label' => $this->randomString(),
|
||||
'style' => 999
|
||||
'style' => 999,
|
||||
]);
|
||||
$entity->save();
|
||||
$this->assertSame('999', $entity->style);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Config;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Config\ConfigImporter;
|
||||
use Drupal\Core\Config\StorageComparer;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
@@ -33,7 +32,7 @@ class ConfigImportRecreateTest extends KernelTestBase {
|
||||
parent::setUp();
|
||||
|
||||
$this->installEntitySchema('node');
|
||||
$this->installConfig(['field', 'node']);
|
||||
$this->installConfig(['system', 'field', 'node']);
|
||||
|
||||
$this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
|
||||
|
||||
@@ -57,7 +56,7 @@ class ConfigImportRecreateTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
public function testRecreateEntity() {
|
||||
$type_name = Unicode::strtolower($this->randomMachineName(16));
|
||||
$type_name = mb_strtolower($this->randomMachineName(16));
|
||||
$content_type = NodeType::create([
|
||||
'type' => $type_name,
|
||||
'name' => 'Node type one',
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Config;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Component\Uuid\Php;
|
||||
use Drupal\Core\Config\ConfigImporter;
|
||||
use Drupal\Core\Config\ConfigImporterException;
|
||||
@@ -40,7 +39,7 @@ class ConfigImportRenameValidationTest extends KernelTestBase {
|
||||
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('node');
|
||||
$this->installConfig(['field']);
|
||||
$this->installConfig(['system', 'field']);
|
||||
|
||||
// Set up the ConfigImporter object for testing.
|
||||
$storage_comparer = new StorageComparer(
|
||||
@@ -82,7 +81,7 @@ class ConfigImportRenameValidationTest extends KernelTestBase {
|
||||
|
||||
// Create a content type with a matching UUID in the active storage.
|
||||
$content_type = NodeType::create([
|
||||
'type' => Unicode::strtolower($this->randomMachineName(16)),
|
||||
'type' => mb_strtolower($this->randomMachineName(16)),
|
||||
'name' => $this->randomMachineName(),
|
||||
'uuid' => $uuid,
|
||||
]);
|
||||
@@ -106,7 +105,7 @@ class ConfigImportRenameValidationTest extends KernelTestBase {
|
||||
catch (ConfigImporterException $e) {
|
||||
$this->pass('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.');
|
||||
$expected = [
|
||||
SafeMarkup::format('Entity type mismatch on rename. @old_type not equal to @new_type for existing configuration @old_name and staged configuration @new_name.', ['@old_type' => 'node_type', '@new_type' => 'config_test', '@old_name' => 'node.type.' . $content_type->id(), '@new_name' => 'config_test.dynamic.' . $test_entity_id])
|
||||
new FormattableMarkup('Entity type mismatch on rename. @old_type not equal to @new_type for existing configuration @old_name and staged configuration @new_name.', ['@old_type' => 'node_type', '@new_type' => 'config_test', '@old_name' => 'node.type.' . $content_type->id(), '@new_name' => 'config_test.dynamic.' . $test_entity_id]),
|
||||
];
|
||||
$this->assertEqual($expected, $this->configImporter->getErrors());
|
||||
}
|
||||
@@ -135,7 +134,7 @@ class ConfigImportRenameValidationTest extends KernelTestBase {
|
||||
// UUIDs match.
|
||||
$this->configImporter->reset();
|
||||
$expected = [
|
||||
'config_test.old::config_test.new'
|
||||
'config_test.old::config_test.new',
|
||||
];
|
||||
$renames = $this->configImporter->getUnprocessedConfiguration('rename');
|
||||
$this->assertSame($expected, $renames);
|
||||
@@ -149,7 +148,7 @@ class ConfigImportRenameValidationTest extends KernelTestBase {
|
||||
catch (ConfigImporterException $e) {
|
||||
$this->pass('Expected ConfigImporterException thrown when simple configuration is renamed.');
|
||||
$expected = [
|
||||
SafeMarkup::format('Rename operation for simple configuration. Existing configuration @old_name and staged configuration @new_name.', ['@old_name' => 'config_test.old', '@new_name' => 'config_test.new'])
|
||||
new FormattableMarkup('Rename operation for simple configuration. Existing configuration @old_name and staged configuration @new_name.', ['@old_name' => 'config_test.old', '@new_name' => 'config_test.new']),
|
||||
];
|
||||
$this->assertEqual($expected, $this->configImporter->getErrors());
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class ConfigImporterMissingContentTest extends KernelTestBase {
|
||||
$this->installSchema('system', 'sequences');
|
||||
$this->installEntitySchema('entity_test');
|
||||
$this->installEntitySchema('user');
|
||||
$this->installConfig(['config_test']);
|
||||
$this->installConfig(['system', 'config_test']);
|
||||
// Installing config_test's default configuration pollutes the global
|
||||
// variable being used for recording hook invocations by this test already,
|
||||
// so it has to be cleared out manually.
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Drupal\KernelTests\Core\Config;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Core\Config\ConfigImporter;
|
||||
use Drupal\Core\Config\ConfigImporterException;
|
||||
use Drupal\Core\Config\StorageComparer;
|
||||
@@ -16,6 +16,11 @@ use Drupal\KernelTests\KernelTestBase;
|
||||
*/
|
||||
class ConfigImporterTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* The beginning of an import validation error.
|
||||
*/
|
||||
const FAIL_MESSAGE = 'There were errors validating the config synchronization.';
|
||||
|
||||
/**
|
||||
* Config Importer object used for testing.
|
||||
*
|
||||
@@ -33,7 +38,7 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installConfig(['config_test']);
|
||||
$this->installConfig(['system', 'config_test']);
|
||||
// Installing config_test's default configuration pollutes the global
|
||||
// variable being used for recording hook invocations by this test already,
|
||||
// so it has to be cleared out manually.
|
||||
@@ -104,10 +109,17 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
$this->fail('ConfigImporterException not thrown, invalid import was not stopped due to mis-matching site UUID.');
|
||||
}
|
||||
catch (ConfigImporterException $e) {
|
||||
$this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.');
|
||||
$error_log = $this->configImporter->getErrors();
|
||||
$expected = ['Site UUID in source storage does not match the target storage.'];
|
||||
$this->assertEqual($expected, $error_log);
|
||||
$actual_message = $e->getMessage();
|
||||
|
||||
$actual_error_log = $this->configImporter->getErrors();
|
||||
$expected_error_log = ['Site UUID in source storage does not match the target storage.'];
|
||||
$this->assertEqual($actual_error_log, $expected_error_log);
|
||||
|
||||
$expected = static::FAIL_MESSAGE . PHP_EOL . 'Site UUID in source storage does not match the target storage.';
|
||||
$this->assertEquals($expected, $actual_message);
|
||||
foreach ($expected_error_log as $log_row) {
|
||||
$this->assertTrue(preg_match("/$log_row/", $actual_message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,7 +238,7 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
// Add a dependency on primary, to ensure that is synced first.
|
||||
'dependencies' => [
|
||||
'config' => [$name_primary],
|
||||
]
|
||||
],
|
||||
];
|
||||
$sync->write($name_secondary, $values_secondary);
|
||||
|
||||
@@ -245,7 +257,7 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
|
||||
$logs = $this->configImporter->getErrors();
|
||||
$this->assertEqual(count($logs), 1);
|
||||
$this->assertEqual($logs[0], SafeMarkup::format('Deleted and replaced configuration entity "@name"', ['@name' => $name_secondary]));
|
||||
$this->assertEqual($logs[0], new FormattableMarkup('Deleted and replaced configuration entity "@name"', ['@name' => $name_secondary]));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,7 +277,7 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
// Add a dependency on secondary, so that is synced first.
|
||||
'dependencies' => [
|
||||
'config' => [$name_secondary],
|
||||
]
|
||||
],
|
||||
];
|
||||
$sync->write($name_primary, $values_primary);
|
||||
$values_secondary = [
|
||||
@@ -322,7 +334,7 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
// Add a dependency on deleter, to make sure that is synced first.
|
||||
'dependencies' => [
|
||||
'config' => [$name_deleter],
|
||||
]
|
||||
],
|
||||
];
|
||||
$storage->write($name_deletee, $values_deletee);
|
||||
$values_deletee['label'] = 'Updated Deletee';
|
||||
@@ -338,7 +350,7 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
// will also be synced after the deletee due to alphabetical ordering.
|
||||
'dependencies' => [
|
||||
'config' => [$name_deleter],
|
||||
]
|
||||
],
|
||||
];
|
||||
$storage->write($name_other, $values_other);
|
||||
$values_other['label'] = 'Updated other';
|
||||
@@ -373,7 +385,7 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
|
||||
$logs = $this->configImporter->getErrors();
|
||||
$this->assertEqual(count($logs), 1);
|
||||
$this->assertEqual($logs[0], SafeMarkup::format('Update target "@name" is missing.', ['@name' => $name_deletee]));
|
||||
$this->assertEqual($logs[0], new FormattableMarkup('Update target "@name" is missing.', ['@name' => $name_deletee]));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -580,7 +592,20 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
$this->fail('ConfigImporterException not thrown; an invalid import was not stopped due to missing dependencies.');
|
||||
}
|
||||
catch (ConfigImporterException $e) {
|
||||
$this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.');
|
||||
$expected = [
|
||||
static::FAIL_MESSAGE,
|
||||
'Unable to install the <em class="placeholder">unknown_module</em> module since it does not exist.',
|
||||
'Unable to install the <em class="placeholder">Book</em> module since it requires the <em class="placeholder">Node, Text, Field, Filter, User</em> modules.',
|
||||
'Unable to install the <em class="placeholder">unknown_theme</em> theme since it does not exist.',
|
||||
'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Classy</em> theme.',
|
||||
'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Stable</em> theme.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.config</em> depends on the <em class="placeholder">unknown</em> configuration that will not exist after import.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.existing</em> depends on the <em class="placeholder">config_test.dynamic.dotted.deleted</em> configuration that will not exist after import.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.module</em> depends on the <em class="placeholder">unknown</em> module that will not be installed after import.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.theme</em> depends on the <em class="placeholder">unknown</em> theme that will not be installed after import.',
|
||||
'Configuration <em class="placeholder">unknown.config</em> depends on the <em class="placeholder">unknown</em> extension that will not be installed after import.',
|
||||
];
|
||||
$this->assertEquals(implode(PHP_EOL, $expected), $e->getMessage());
|
||||
$error_log = $this->configImporter->getErrors();
|
||||
$expected = [
|
||||
'Unable to install the <em class="placeholder">unknown_module</em> module since it does not exist.',
|
||||
@@ -611,7 +636,30 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
$this->fail('ConfigImporterException not thrown, invalid import was not stopped due to missing dependencies.');
|
||||
}
|
||||
catch (ConfigImporterException $e) {
|
||||
$this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.');
|
||||
$expected = [
|
||||
static::FAIL_MESSAGE,
|
||||
'Unable to install the <em class="placeholder">unknown_module</em> module since it does not exist.',
|
||||
'Unable to install the <em class="placeholder">Book</em> module since it requires the <em class="placeholder">Node, Text, Field, Filter, User</em> modules.',
|
||||
'Unable to install the <em class="placeholder">unknown_theme</em> theme since it does not exist.',
|
||||
'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Classy</em> theme.',
|
||||
'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Stable</em> theme.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.config</em> depends on the <em class="placeholder">unknown</em> configuration that will not exist after import.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.existing</em> depends on the <em class="placeholder">config_test.dynamic.dotted.deleted</em> configuration that will not exist after import.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.module</em> depends on the <em class="placeholder">unknown</em> module that will not be installed after import.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.theme</em> depends on the <em class="placeholder">unknown</em> theme that will not be installed after import.',
|
||||
'Configuration <em class="placeholder">unknown.config</em> depends on the <em class="placeholder">unknown</em> extension that will not be installed after import.',
|
||||
'Unable to install the <em class="placeholder">unknown_module</em> module since it does not exist.',
|
||||
'Unable to install the <em class="placeholder">Book</em> module since it requires the <em class="placeholder">Node, Text, Field, Filter, User</em> modules.',
|
||||
'Unable to install the <em class="placeholder">unknown_theme</em> theme since it does not exist.',
|
||||
'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Classy</em> theme.',
|
||||
'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Stable</em> theme.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.config</em> depends on configuration (<em class="placeholder">unknown, unknown2</em>) that will not exist after import.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.existing</em> depends on the <em class="placeholder">config_test.dynamic.dotted.deleted</em> configuration that will not exist after import.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.module</em> depends on modules (<em class="placeholder">unknown, Database Logging</em>) that will not be installed after import.',
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.theme</em> depends on themes (<em class="placeholder">unknown, Seven</em>) that will not be installed after import.',
|
||||
'Configuration <em class="placeholder">unknown.config</em> depends on the <em class="placeholder">unknown</em> extension that will not be installed after import.',
|
||||
];
|
||||
$this->assertEquals(implode(PHP_EOL, $expected), $e->getMessage());
|
||||
$error_log = $this->configImporter->getErrors();
|
||||
$expected = [
|
||||
'Configuration <em class="placeholder">config_test.dynamic.dotted.config</em> depends on configuration (<em class="placeholder">unknown, unknown2</em>) that will not exist after import.',
|
||||
@@ -637,7 +685,8 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
$this->fail('ConfigImporterException not thrown, invalid import was not stopped due to missing dependencies.');
|
||||
}
|
||||
catch (ConfigImporterException $e) {
|
||||
$this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.');
|
||||
$expected = static::FAIL_MESSAGE . PHP_EOL . 'The core.extension configuration does not exist.';
|
||||
$this->assertEquals($expected, $e->getMessage());
|
||||
$error_log = $this->configImporter->getErrors();
|
||||
$this->assertEqual(['The core.extension configuration does not exist.'], $error_log);
|
||||
}
|
||||
@@ -661,7 +710,8 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
$this->fail('ConfigImporterException not thrown; an invalid import was not stopped due to missing dependencies.');
|
||||
}
|
||||
catch (ConfigImporterException $e) {
|
||||
$this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.');
|
||||
$expected = static::FAIL_MESSAGE . PHP_EOL . 'Unable to install the <em class="placeholder">standard</em> module since it does not exist.';
|
||||
$this->assertEquals($expected, $e->getMessage(), 'There were errors validating the config synchronization.');
|
||||
$error_log = $this->configImporter->getErrors();
|
||||
// Install profiles should not even be scanned at this point.
|
||||
$this->assertEqual(['Unable to install the <em class="placeholder">standard</em> module since it does not exist.'], $error_log);
|
||||
@@ -686,7 +736,8 @@ class ConfigImporterTest extends KernelTestBase {
|
||||
$this->fail('ConfigImporterException not thrown; an invalid import was not stopped due to missing dependencies.');
|
||||
}
|
||||
catch (ConfigImporterException $e) {
|
||||
$this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.');
|
||||
$expected = static::FAIL_MESSAGE . PHP_EOL . 'Cannot change the install profile from <em class="placeholder"></em> to <em class="placeholder">this_will_not_work</em> once Drupal is installed.';
|
||||
$this->assertEquals($expected, $e->getMessage(), 'There were errors validating the config synchronization.');
|
||||
$error_log = $this->configImporter->getErrors();
|
||||
// Install profiles can not be changed. Note that KernelTestBase currently
|
||||
// does not use an install profile. This situation should be impossible
|
||||
|
||||
@@ -253,6 +253,14 @@ class ConfigInstallTest extends KernelTestBase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests installing configuration where the filename and ID do not match.
|
||||
*/
|
||||
public function testIdMisMatch() {
|
||||
$this->setExpectedException(\PHPUnit_Framework_Error_Warning::class, 'The configuration name "config_test.dynamic.no_id_match" does not match the ID "does_not_match"');
|
||||
$this->installModules(['config_test_id_mismatch']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs a module.
|
||||
*
|
||||
|
||||
@@ -20,6 +20,7 @@ class ConfigOverrideTest extends KernelTestBase {
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(['system']);
|
||||
$this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
|
||||
}
|
||||
|
||||
|
||||
@@ -352,7 +352,7 @@ class ConfigSchemaTest extends KernelTestBase {
|
||||
// If the config schema doesn't have a type it shouldn't be casted.
|
||||
'no_type' => 1,
|
||||
'mapping' => [
|
||||
'string' => 1
|
||||
'string' => 1,
|
||||
],
|
||||
'float' => '3.14',
|
||||
'null_float' => '',
|
||||
@@ -375,7 +375,7 @@ class ConfigSchemaTest extends KernelTestBase {
|
||||
'boolean' => TRUE,
|
||||
'no_type' => 1,
|
||||
'mapping' => [
|
||||
'string' => '1'
|
||||
'string' => '1',
|
||||
],
|
||||
'float' => 3.14,
|
||||
'null_float' => NULL,
|
||||
|
||||
@@ -24,6 +24,7 @@ class ConfigSnapshotTest extends KernelTestBase {
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(['system']);
|
||||
// Update the config snapshot. This allows the parent::setUp() to write
|
||||
// configuration files.
|
||||
\Drupal::service('config.manager')->createSnapshot(\Drupal::service('config.storage'), \Drupal::service('config.storage.snapshot'));
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Config\Entity;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
|
||||
use Drupal\Core\Site\Settings;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests \Drupal\Core\Config\Entity\ConfigEntityUpdater.
|
||||
*
|
||||
* @coversDefaultClass \Drupal\Core\Config\Entity\ConfigEntityUpdater
|
||||
* @group config
|
||||
*/
|
||||
class ConfigEntityUpdaterTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['config_test'];
|
||||
|
||||
/**
|
||||
* @covers ::update
|
||||
*/
|
||||
public function testUpdate() {
|
||||
// Create some entities to update.
|
||||
$storage = $this->container->get('entity_type.manager')->getStorage('config_test');
|
||||
for ($i = 0; $i < 15; $i++) {
|
||||
$entity_id = 'config_test_' . $i;
|
||||
$storage->create(['id' => $entity_id, 'label' => $entity_id])->save();
|
||||
}
|
||||
|
||||
// Set up the updater.
|
||||
$sandbox = [];
|
||||
$settings = Settings::getInstance() ? Settings::getAll() : [];
|
||||
$settings['entity_update_batch_size'] = 10;
|
||||
new Settings($settings);
|
||||
$updater = $this->container->get('class_resolver')->getInstanceFromDefinition(ConfigEntityUpdater::class);
|
||||
|
||||
$callback = function ($config_entity) {
|
||||
/** @var \Drupal\config_test\Entity\ConfigTest $config_entity */
|
||||
$number = (int) str_replace('config_test_', '', $config_entity->id());
|
||||
// Only update even numbered entities.
|
||||
if ($number % 2 == 0) {
|
||||
$config_entity->set('label', $config_entity->label . ' (updated)');
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
};
|
||||
|
||||
// This should run against the first 10 entities. The even numbered labels
|
||||
// will have been updated.
|
||||
$updater->update($sandbox, 'config_test', $callback);
|
||||
$entities = $storage->loadMultiple();
|
||||
$this->assertEquals('config_test_8 (updated)', $entities['config_test_8']->label());
|
||||
$this->assertEquals('config_test_9', $entities['config_test_9']->label());
|
||||
$this->assertEquals('config_test_10', $entities['config_test_10']->label());
|
||||
$this->assertEquals('config_test_14', $entities['config_test_14']->label());
|
||||
$this->assertEquals(15, $sandbox['config_entity_updater:config_test']['count']);
|
||||
$this->assertCount(5, $sandbox['config_entity_updater:config_test']['entities']);
|
||||
$this->assertEquals(10 / 15, $sandbox['#finished']);
|
||||
|
||||
// Update the rest.
|
||||
$updater->update($sandbox, 'config_test', $callback);
|
||||
$entities = $storage->loadMultiple();
|
||||
$this->assertEquals('config_test_8 (updated)', $entities['config_test_8']->label());
|
||||
$this->assertEquals('config_test_9', $entities['config_test_9']->label());
|
||||
$this->assertEquals('config_test_10 (updated)', $entities['config_test_10']->label());
|
||||
$this->assertEquals('config_test_14 (updated)', $entities['config_test_14']->label());
|
||||
$this->assertEquals(1, $sandbox['#finished']);
|
||||
$this->assertCount(0, $sandbox['config_entity_updater:config_test']['entities']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::update
|
||||
*/
|
||||
public function testUpdateDefaultCallback() {
|
||||
// Create some entities to update.
|
||||
$storage = $this->container->get('entity_type.manager')->getStorage('config_test');
|
||||
for ($i = 0; $i < 15; $i++) {
|
||||
$entity_id = 'config_test_' . $i;
|
||||
$storage->create(['id' => $entity_id, 'label' => $entity_id])->save();
|
||||
}
|
||||
|
||||
// Set up the updater.
|
||||
$sandbox = [];
|
||||
$settings = Settings::getInstance() ? Settings::getAll() : [];
|
||||
$settings['entity_update_batch_size'] = 9;
|
||||
new Settings($settings);
|
||||
$updater = $this->container->get('class_resolver')->getInstanceFromDefinition(ConfigEntityUpdater::class);
|
||||
// Cause a dependency to be added during an update.
|
||||
\Drupal::state()->set('config_test_new_dependency', 'added_dependency');
|
||||
|
||||
// This should run against the first 10 entities.
|
||||
$updater->update($sandbox, 'config_test');
|
||||
$entities = $storage->loadMultiple();
|
||||
$this->assertEquals(['added_dependency'], $entities['config_test_7']->getDependencies()['module']);
|
||||
$this->assertEquals(['added_dependency'], $entities['config_test_8']->getDependencies()['module']);
|
||||
$this->assertEquals([], $entities['config_test_9']->getDependencies());
|
||||
$this->assertEquals([], $entities['config_test_14']->getDependencies());
|
||||
$this->assertEquals(15, $sandbox['config_entity_updater:config_test']['count']);
|
||||
$this->assertCount(6, $sandbox['config_entity_updater:config_test']['entities']);
|
||||
$this->assertEquals(9 / 15, $sandbox['#finished']);
|
||||
|
||||
// Update the rest.
|
||||
$updater->update($sandbox, 'config_test');
|
||||
$entities = $storage->loadMultiple();
|
||||
$this->assertEquals(['added_dependency'], $entities['config_test_9']->getDependencies()['module']);
|
||||
$this->assertEquals(['added_dependency'], $entities['config_test_14']->getDependencies()['module']);
|
||||
$this->assertEquals(1, $sandbox['#finished']);
|
||||
$this->assertCount(0, $sandbox['config_entity_updater:config_test']['entities']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::update
|
||||
*/
|
||||
public function testUpdateException() {
|
||||
$this->enableModules(['entity_test']);
|
||||
$this->setExpectedException(\InvalidArgumentException::class, 'The provided entity type ID \'entity_test_mul_changed\' is not a configuration entity type');
|
||||
$updater = $this->container->get('class_resolver')->getInstanceFromDefinition(ConfigEntityUpdater::class);
|
||||
$sandbox = [];
|
||||
$updater->update($sandbox, 'entity_test_mul_changed');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,6 +12,7 @@ namespace Drupal\KernelTests\Core\Database;
|
||||
* @group Database
|
||||
*/
|
||||
class BasicSyntaxTest extends DatabaseTestBase {
|
||||
|
||||
/**
|
||||
* Tests string concatenation.
|
||||
*/
|
||||
@@ -28,15 +29,22 @@ class BasicSyntaxTest extends DatabaseTestBase {
|
||||
|
||||
/**
|
||||
* Tests string concatenation with field values.
|
||||
*
|
||||
* We use 'job' and 'age' fields from the {test} table. Using the 'name' field
|
||||
* for concatenation causes issues with custom or contrib database drivers,
|
||||
* since its type 'varchar_ascii' may lead to using field-level collations not
|
||||
* compatible with the other fields.
|
||||
*/
|
||||
public function testConcatFields() {
|
||||
$result = db_query('SELECT CONCAT(:a1, CONCAT(name, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', [
|
||||
':a1' => 'The age of ',
|
||||
':a2' => ' is ',
|
||||
':a3' => '.',
|
||||
':age' => 25,
|
||||
]);
|
||||
$this->assertIdentical($result->fetchField(), 'The age of John is 25.', 'Field CONCAT works.');
|
||||
$result = $this->connection->query(
|
||||
'SELECT CONCAT(:a1, CONCAT(job, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', [
|
||||
':a1' => 'The age of ',
|
||||
':a2' => ' is ',
|
||||
':a3' => '.',
|
||||
':age' => 25,
|
||||
]
|
||||
);
|
||||
$this->assertSame('The age of Singer is 25.', $result->fetchField(), 'Field CONCAT works.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Drupal\KernelTests\Core\Database;
|
||||
* @group Database
|
||||
*/
|
||||
class CaseSensitivityTest extends DatabaseTestBase {
|
||||
|
||||
/**
|
||||
* Tests BINARY collation in MySQL.
|
||||
*/
|
||||
|
||||
@@ -63,7 +63,7 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getConnectionID() {
|
||||
protected function getConnectionId() {
|
||||
return (int) Database::getConnection($this->target, $this->key)->query('SELECT CONNECTION_ID()')->fetchField();
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||
/**
|
||||
* Tests Database::closeConnection() without query.
|
||||
*
|
||||
* @todo getConnectionID() executes a query.
|
||||
* @todo getConnectionId() executes a query.
|
||||
*/
|
||||
public function testOpenClose() {
|
||||
if ($this->skipTest) {
|
||||
@@ -100,7 +100,7 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||
}
|
||||
// Add and open a new connection.
|
||||
$this->addConnection();
|
||||
$id = $this->getConnectionID();
|
||||
$id = $this->getConnectionId();
|
||||
Database::getConnection($this->target, $this->key);
|
||||
|
||||
// Verify that there is a new connection.
|
||||
@@ -124,7 +124,7 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||
}
|
||||
// Add and open a new connection.
|
||||
$this->addConnection();
|
||||
$id = $this->getConnectionID();
|
||||
$id = $this->getConnectionId();
|
||||
Database::getConnection($this->target, $this->key);
|
||||
|
||||
// Verify that there is a new connection.
|
||||
@@ -151,7 +151,7 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||
}
|
||||
// Add and open a new connection.
|
||||
$this->addConnection();
|
||||
$id = $this->getConnectionID();
|
||||
$id = $this->getConnectionId();
|
||||
Database::getConnection($this->target, $this->key);
|
||||
|
||||
// Verify that there is a new connection.
|
||||
@@ -178,7 +178,7 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||
}
|
||||
// Add and open a new connection.
|
||||
$this->addConnection();
|
||||
$id = $this->getConnectionID();
|
||||
$id = $this->getConnectionId();
|
||||
Database::getConnection($this->target, $this->key);
|
||||
|
||||
// Verify that there is a new connection.
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Database;
|
||||
|
||||
/**
|
||||
* Deprecation tests cases for the database layer.
|
||||
*
|
||||
* @group legacy
|
||||
*/
|
||||
class DatabaseLegacyTest extends DatabaseTestBase {
|
||||
|
||||
/**
|
||||
* Tests the db_table_exists() function.
|
||||
*
|
||||
* @expectedDeprecation db_table_exists() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use $injected_database->schema()->tableExists($table) instead. See https://www.drupal.org/node/2947929.
|
||||
*/
|
||||
public function testDbTableExists() {
|
||||
$this->assertTrue(db_table_exists('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the db_set_active() function.
|
||||
*
|
||||
* @expectedDeprecation db_set_active() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\Database::setActiveConnection() instead. See https://www.drupal.org/node/2944084.
|
||||
*/
|
||||
public function testDbSetActive() {
|
||||
$get_active_db = $this->connection->getKey();
|
||||
$this->assert(db_set_active($get_active_db), 'Database connection is active');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the db_drop_table() function.
|
||||
*
|
||||
* @expectedDeprecation db_drop_table() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\Database::getConnection()->schema()->dropTable() instead. See https://www.drupal.org/node/2987737
|
||||
*/
|
||||
public function testDbDropTable() {
|
||||
$this->assertFalse(db_drop_table('temp_test_table'));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Database;
|
||||
|
||||
use Drupal\Core\Database\Database;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
@@ -14,8 +15,16 @@ abstract class DatabaseTestBase extends KernelTestBase {
|
||||
|
||||
public static $modules = ['database_test'];
|
||||
|
||||
/**
|
||||
* The database connection for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Database\Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->connection = Database::getConnection();
|
||||
$this->installSchema('database_test', [
|
||||
'test',
|
||||
'test_people',
|
||||
@@ -35,7 +44,7 @@ abstract class DatabaseTestBase extends KernelTestBase {
|
||||
* Sets up tables for NULL handling.
|
||||
*/
|
||||
public function ensureSampleDataNull() {
|
||||
db_insert('test_null')
|
||||
$this->connection->insert('test_null')
|
||||
->fields(['name', 'age'])
|
||||
->values([
|
||||
'name' => 'Kermit',
|
||||
@@ -56,8 +65,10 @@ abstract class DatabaseTestBase extends KernelTestBase {
|
||||
* Sets up our sample data.
|
||||
*/
|
||||
public static function addSampleData() {
|
||||
$connection = Database::getConnection();
|
||||
|
||||
// We need the IDs, so we can't use a multi-insert here.
|
||||
$john = db_insert('test')
|
||||
$john = $connection->insert('test')
|
||||
->fields([
|
||||
'name' => 'John',
|
||||
'age' => 25,
|
||||
@@ -65,7 +76,7 @@ abstract class DatabaseTestBase extends KernelTestBase {
|
||||
])
|
||||
->execute();
|
||||
|
||||
$george = db_insert('test')
|
||||
$george = $connection->insert('test')
|
||||
->fields([
|
||||
'name' => 'George',
|
||||
'age' => 27,
|
||||
@@ -73,7 +84,7 @@ abstract class DatabaseTestBase extends KernelTestBase {
|
||||
])
|
||||
->execute();
|
||||
|
||||
db_insert('test')
|
||||
$connection->insert('test')
|
||||
->fields([
|
||||
'name' => 'Ringo',
|
||||
'age' => 28,
|
||||
@@ -81,7 +92,7 @@ abstract class DatabaseTestBase extends KernelTestBase {
|
||||
])
|
||||
->execute();
|
||||
|
||||
$paul = db_insert('test')
|
||||
$paul = $connection->insert('test')
|
||||
->fields([
|
||||
'name' => 'Paul',
|
||||
'age' => 26,
|
||||
@@ -89,7 +100,7 @@ abstract class DatabaseTestBase extends KernelTestBase {
|
||||
])
|
||||
->execute();
|
||||
|
||||
db_insert('test_people')
|
||||
$connection->insert('test_people')
|
||||
->fields([
|
||||
'name' => 'Meredith',
|
||||
'age' => 30,
|
||||
@@ -97,7 +108,7 @@ abstract class DatabaseTestBase extends KernelTestBase {
|
||||
])
|
||||
->execute();
|
||||
|
||||
db_insert('test_task')
|
||||
$connection->insert('test_task')
|
||||
->fields(['pid', 'task', 'priority'])
|
||||
->values([
|
||||
'pid' => $john,
|
||||
@@ -136,10 +147,11 @@ abstract class DatabaseTestBase extends KernelTestBase {
|
||||
])
|
||||
->execute();
|
||||
|
||||
db_insert('test_special_columns')
|
||||
$connection->insert('test_special_columns')
|
||||
->fields([
|
||||
'id' => 1,
|
||||
'offset' => 'Offset value 1',
|
||||
'function' => 'Function value 1',
|
||||
])
|
||||
->execute();
|
||||
}
|
||||
|
||||
@@ -66,6 +66,84 @@ class DeleteTruncateTest extends DatabaseTestBase {
|
||||
$this->assertEqual(0, $num_records_after, 'Truncate really deletes everything.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms that we can truncate a whole table while in transaction.
|
||||
*/
|
||||
public function testTruncateInTransaction() {
|
||||
// This test won't work right if transactions are not supported.
|
||||
if (!$this->connection->supportsTransactions()) {
|
||||
$this->markTestSkipped('The database driver does not support transactions.');
|
||||
}
|
||||
|
||||
$num_records_before = $this->connection->select('test')->countQuery()->execute()->fetchField();
|
||||
$this->assertGreaterThan(0, $num_records_before, 'The table is not empty.');
|
||||
|
||||
$transaction = $this->connection->startTransaction('test_truncate_in_transaction');
|
||||
$this->connection->insert('test')
|
||||
->fields([
|
||||
'name' => 'Freddie',
|
||||
'age' => 45,
|
||||
'job' => 'Great singer',
|
||||
])
|
||||
->execute();
|
||||
$num_records_after_insert = $this->connection->select('test')->countQuery()->execute()->fetchField();
|
||||
$this->assertEquals($num_records_before + 1, $num_records_after_insert);
|
||||
|
||||
$this->connection->truncate('test')->execute();
|
||||
|
||||
// Checks that there are no records left in the table, and transaction is
|
||||
// still active.
|
||||
$this->assertTrue($this->connection->inTransaction());
|
||||
$num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
|
||||
$this->assertEquals(0, $num_records_after);
|
||||
|
||||
// Close the transaction, and check that there are still no records in the
|
||||
// table.
|
||||
$transaction = NULL;
|
||||
$this->assertFalse($this->connection->inTransaction());
|
||||
$num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
|
||||
$this->assertEquals(0, $num_records_after);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms that transaction rollback voids a truncate operation.
|
||||
*/
|
||||
public function testTruncateTransactionRollback() {
|
||||
// This test won't work right if transactions are not supported.
|
||||
if (!$this->connection->supportsTransactions()) {
|
||||
$this->markTestSkipped('The database driver does not support transactions.');
|
||||
}
|
||||
|
||||
$num_records_before = $this->connection->select('test')->countQuery()->execute()->fetchField();
|
||||
$this->assertGreaterThan(0, $num_records_before, 'The table is not empty.');
|
||||
|
||||
$transaction = $this->connection->startTransaction('test_truncate_in_transaction');
|
||||
$this->connection->insert('test')
|
||||
->fields([
|
||||
'name' => 'Freddie',
|
||||
'age' => 45,
|
||||
'job' => 'Great singer',
|
||||
])
|
||||
->execute();
|
||||
$num_records_after_insert = $this->connection->select('test')->countQuery()->execute()->fetchField();
|
||||
$this->assertEquals($num_records_before + 1, $num_records_after_insert);
|
||||
|
||||
$this->connection->truncate('test')->execute();
|
||||
|
||||
// Checks that there are no records left in the table, and transaction is
|
||||
// still active.
|
||||
$this->assertTrue($this->connection->inTransaction());
|
||||
$num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
|
||||
$this->assertEquals(0, $num_records_after);
|
||||
|
||||
// Roll back the transaction, and check that we are back to status before
|
||||
// insert and truncate.
|
||||
$this->connection->rollBack();
|
||||
$this->assertFalse($this->connection->inTransaction());
|
||||
$num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
|
||||
$this->assertEquals($num_records_before, $num_records_after);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms that we can delete a single special column name record successfully.
|
||||
*/
|
||||
|
||||
@@ -198,14 +198,20 @@ class InsertTest extends DatabaseTestBase {
|
||||
* Tests that we can INSERT INTO a special named column.
|
||||
*/
|
||||
public function testSpecialColumnInsert() {
|
||||
$id = db_insert('test_special_columns')
|
||||
$this->connection->insert('test_special_columns')
|
||||
->fields([
|
||||
'id' => 2,
|
||||
'offset' => 'Offset value 2',
|
||||
'function' => 'foobar',
|
||||
])
|
||||
->execute();
|
||||
$saved_value = db_query('SELECT "offset" FROM {test_special_columns} WHERE id = :id', [':id' => 2])->fetchField();
|
||||
$this->assertIdentical($saved_value, 'Offset value 2', 'Can retrieve special column name value after inserting.');
|
||||
$result = $this->connection->select('test_special_columns')
|
||||
->fields('test_special_columns', ['offset', 'function'])
|
||||
->condition('test_special_columns.function', 'foobar')
|
||||
->execute();
|
||||
$record = $result->fetch();
|
||||
$this->assertSame('Offset value 2', $record->offset);
|
||||
$this->assertSame('foobar', $record->function);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ use Drupal\Core\Database\IntegrityConstraintViolationException;
|
||||
* @group Database
|
||||
*/
|
||||
class InvalidDataTest extends DatabaseTestBase {
|
||||
|
||||
/**
|
||||
* Tests aborting of traditional SQL database systems with invalid data.
|
||||
*/
|
||||
|
||||
@@ -113,11 +113,11 @@ class LoggingTest extends DatabaseTestBase {
|
||||
|
||||
db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
|
||||
|
||||
$old_key = db_set_active('test2');
|
||||
$old_key = Database::setActiveConnection('test2');
|
||||
|
||||
db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'], ['target' => 'replica'])->fetchCol();
|
||||
|
||||
db_set_active($old_key);
|
||||
Database::setActiveConnection($old_key);
|
||||
|
||||
$queries1 = Database::getLog('testing1');
|
||||
$queries2 = Database::getLog('testing1', 'test2');
|
||||
|
||||
@@ -37,8 +37,8 @@ class RegressionTest extends DatabaseTestBase {
|
||||
* Tests the db_table_exists() function.
|
||||
*/
|
||||
public function testDBTableExists() {
|
||||
$this->assertSame(TRUE, db_table_exists('test'), 'Returns true for existent table.');
|
||||
$this->assertSame(FALSE, db_table_exists('nosuchtable'), 'Returns false for nonexistent table.');
|
||||
$this->assertSame(TRUE, $this->connection->schema()->tableExists('test'), 'Returns true for existent table.');
|
||||
$this->assertSame(FALSE, $this->connection->schema()->tableExists('nosuchtable'), 'Returns false for nonexistent table.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,15 +12,42 @@ use Drupal\Component\Utility\Unicode;
|
||||
/**
|
||||
* Tests table creation and modification via the schema API.
|
||||
*
|
||||
* @coversDefaultClass \Drupal\Core\Database\Schema
|
||||
*
|
||||
* @group Database
|
||||
*/
|
||||
class SchemaTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* A global counter for table and field creation.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $counter;
|
||||
|
||||
/**
|
||||
* Connection to the database.
|
||||
*
|
||||
* @var \Drupal\Core\Database\Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* Database schema instance.
|
||||
*
|
||||
* @var \Drupal\Core\Database\Schema
|
||||
*/
|
||||
protected $schema;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->connection = Database::getConnection();
|
||||
$this->schema = $this->connection->schema();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests database interactions.
|
||||
*/
|
||||
@@ -52,10 +79,10 @@ class SchemaTest extends KernelTestBase {
|
||||
],
|
||||
],
|
||||
];
|
||||
db_create_table('test_table', $table_specification);
|
||||
$this->schema->createTable('test_table', $table_specification);
|
||||
|
||||
// Assert that the table exists.
|
||||
$this->assertTrue(db_table_exists('test_table'), 'The table exists.');
|
||||
$this->assertTrue($this->schema->tableExists('test_table'), 'The table exists.');
|
||||
|
||||
// Assert that the table comment has been set.
|
||||
$this->checkSchemaComment($table_specification['description'], 'test_table');
|
||||
@@ -63,12 +90,12 @@ class SchemaTest extends KernelTestBase {
|
||||
// Assert that the column comment has been set.
|
||||
$this->checkSchemaComment($table_specification['fields']['test_field']['description'], 'test_table', 'test_field');
|
||||
|
||||
if (Database::getConnection()->databaseType() == 'mysql') {
|
||||
if ($this->connection->databaseType() === 'mysql') {
|
||||
// Make sure that varchar fields have the correct collation.
|
||||
$columns = db_query('SHOW FULL COLUMNS FROM {test_table}');
|
||||
$columns = $this->connection->query('SHOW FULL COLUMNS FROM {test_table}');
|
||||
foreach ($columns as $column) {
|
||||
if ($column->Field == 'test_field_string') {
|
||||
$string_check = ($column->Collation == 'utf8mb4_general_ci');
|
||||
$string_check = ($column->Collation == 'utf8mb4_general_ci' || $column->Collation == 'utf8mb4_0900_ai_ci');
|
||||
}
|
||||
if ($column->Field == 'test_field_string_ascii') {
|
||||
$string_ascii_check = ($column->Collation == 'ascii_general_ci');
|
||||
@@ -82,91 +109,95 @@ class SchemaTest extends KernelTestBase {
|
||||
$this->assertFalse($this->tryInsert(), 'Insert without a default failed.');
|
||||
|
||||
// Add a default value to the column.
|
||||
db_field_set_default('test_table', 'test_field', 0);
|
||||
$this->schema->fieldSetDefault('test_table', 'test_field', 0);
|
||||
// The insert should now succeed.
|
||||
$this->assertTrue($this->tryInsert(), 'Insert with a default succeeded.');
|
||||
|
||||
// Remove the default.
|
||||
db_field_set_no_default('test_table', 'test_field');
|
||||
$this->schema->fieldSetNoDefault('test_table', 'test_field');
|
||||
// The insert should fail again.
|
||||
$this->assertFalse($this->tryInsert(), 'Insert without a default failed.');
|
||||
|
||||
// Test for fake index and test for the boolean result of indexExists().
|
||||
$index_exists = Database::getConnection()->schema()->indexExists('test_table', 'test_field');
|
||||
$this->assertIdentical($index_exists, FALSE, 'Fake index does not exists');
|
||||
$index_exists = $this->schema->indexExists('test_table', 'test_field');
|
||||
$this->assertIdentical($index_exists, FALSE, 'Fake index does not exist');
|
||||
// Add index.
|
||||
db_add_index('test_table', 'test_field', ['test_field'], $table_specification);
|
||||
$this->schema->addIndex('test_table', 'test_field', ['test_field'], $table_specification);
|
||||
// Test for created index and test for the boolean result of indexExists().
|
||||
$index_exists = Database::getConnection()->schema()->indexExists('test_table', 'test_field');
|
||||
$index_exists = $this->schema->indexExists('test_table', 'test_field');
|
||||
$this->assertIdentical($index_exists, TRUE, 'Index created.');
|
||||
|
||||
// Rename the table.
|
||||
db_rename_table('test_table', 'test_table2');
|
||||
$this->schema->renameTable('test_table', 'test_table2');
|
||||
|
||||
// Index should be renamed.
|
||||
$index_exists = Database::getConnection()->schema()->indexExists('test_table2', 'test_field');
|
||||
$index_exists = $this->schema->indexExists('test_table2', 'test_field');
|
||||
$this->assertTrue($index_exists, 'Index was renamed.');
|
||||
|
||||
// We need the default so that we can insert after the rename.
|
||||
db_field_set_default('test_table2', 'test_field', 0);
|
||||
$this->schema->fieldSetDefault('test_table2', 'test_field', 0);
|
||||
$this->assertFalse($this->tryInsert(), 'Insert into the old table failed.');
|
||||
$this->assertTrue($this->tryInsert('test_table2'), 'Insert into the new table succeeded.');
|
||||
|
||||
// We should have successfully inserted exactly two rows.
|
||||
$count = db_query('SELECT COUNT(*) FROM {test_table2}')->fetchField();
|
||||
$count = $this->connection->query('SELECT COUNT(*) FROM {test_table2}')->fetchField();
|
||||
$this->assertEqual($count, 2, 'Two fields were successfully inserted.');
|
||||
|
||||
// Try to drop the table.
|
||||
db_drop_table('test_table2');
|
||||
$this->assertFalse(db_table_exists('test_table2'), 'The dropped table does not exist.');
|
||||
$this->schema->dropTable('test_table2');
|
||||
$this->assertFalse($this->schema->tableExists('test_table2'), 'The dropped table does not exist.');
|
||||
|
||||
// Recreate the table.
|
||||
db_create_table('test_table', $table_specification);
|
||||
db_field_set_default('test_table', 'test_field', 0);
|
||||
db_add_field('test_table', 'test_serial', ['type' => 'int', 'not null' => TRUE, 'default' => 0, 'description' => 'Added column description.']);
|
||||
$this->schema->createTable('test_table', $table_specification);
|
||||
$this->schema->fieldSetDefault('test_table', 'test_field', 0);
|
||||
$this->schema->addField('test_table', 'test_serial', ['type' => 'int', 'not null' => TRUE, 'default' => 0, 'description' => 'Added column description.']);
|
||||
|
||||
// Assert that the column comment has been set.
|
||||
$this->checkSchemaComment('Added column description.', 'test_table', 'test_serial');
|
||||
|
||||
// Change the new field to a serial column.
|
||||
db_change_field('test_table', 'test_serial', 'test_serial', ['type' => 'serial', 'not null' => TRUE, 'description' => 'Changed column description.'], ['primary key' => ['test_serial']]);
|
||||
$this->schema->changeField('test_table', 'test_serial', 'test_serial', ['type' => 'serial', 'not null' => TRUE, 'description' => 'Changed column description.'], ['primary key' => ['test_serial']]);
|
||||
|
||||
// Assert that the column comment has been set.
|
||||
$this->checkSchemaComment('Changed column description.', 'test_table', 'test_serial');
|
||||
|
||||
$this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
|
||||
$max1 = db_query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
|
||||
$max1 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
|
||||
$this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
|
||||
$max2 = db_query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
|
||||
$max2 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
|
||||
$this->assertTrue($max2 > $max1, 'The serial is monotone.');
|
||||
|
||||
$count = db_query('SELECT COUNT(*) FROM {test_table}')->fetchField();
|
||||
$count = $this->connection->query('SELECT COUNT(*) FROM {test_table}')->fetchField();
|
||||
$this->assertEqual($count, 2, 'There were two rows.');
|
||||
|
||||
// Test adding a serial field to an existing table.
|
||||
db_drop_table('test_table');
|
||||
db_create_table('test_table', $table_specification);
|
||||
db_field_set_default('test_table', 'test_field', 0);
|
||||
db_add_field('test_table', 'test_serial', ['type' => 'serial', 'not null' => TRUE], ['primary key' => ['test_serial']]);
|
||||
$this->schema->dropTable('test_table');
|
||||
$this->schema->createTable('test_table', $table_specification);
|
||||
$this->schema->fieldSetDefault('test_table', 'test_field', 0);
|
||||
$this->schema->addField('test_table', 'test_serial', ['type' => 'serial', 'not null' => TRUE], ['primary key' => ['test_serial']]);
|
||||
|
||||
$this->assertPrimaryKeyColumns('test_table', ['test_serial']);
|
||||
// Test the primary key columns.
|
||||
$method = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
|
||||
$method->setAccessible(TRUE);
|
||||
$this->assertSame(['test_serial'], $method->invoke($this->schema, 'test_table'));
|
||||
|
||||
$this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
|
||||
$max1 = db_query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
|
||||
$max1 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
|
||||
$this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.');
|
||||
$max2 = db_query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
|
||||
$max2 = $this->connection->query('SELECT MAX(test_serial) FROM {test_table}')->fetchField();
|
||||
$this->assertTrue($max2 > $max1, 'The serial is monotone.');
|
||||
|
||||
$count = db_query('SELECT COUNT(*) FROM {test_table}')->fetchField();
|
||||
$count = $this->connection->query('SELECT COUNT(*) FROM {test_table}')->fetchField();
|
||||
$this->assertEqual($count, 2, 'There were two rows.');
|
||||
|
||||
// Test adding a new column and form a composite primary key with it.
|
||||
db_add_field('test_table', 'test_composite_primary_key', ['type' => 'int', 'not null' => TRUE, 'default' => 0], ['primary key' => ['test_serial', 'test_composite_primary_key']]);
|
||||
$this->schema->addField('test_table', 'test_composite_primary_key', ['type' => 'int', 'not null' => TRUE, 'default' => 0], ['primary key' => ['test_serial', 'test_composite_primary_key']]);
|
||||
|
||||
$this->assertPrimaryKeyColumns('test_table', ['test_serial', 'test_composite_primary_key']);
|
||||
// Test the primary key columns.
|
||||
$this->assertSame(['test_serial', 'test_composite_primary_key'], $method->invoke($this->schema, 'test_table'));
|
||||
|
||||
// Test renaming of keys and constraints.
|
||||
db_drop_table('test_table');
|
||||
$this->schema->dropTable('test_table');
|
||||
$table_specification = [
|
||||
'fields' => [
|
||||
'id' => [
|
||||
@@ -183,46 +214,50 @@ class SchemaTest extends KernelTestBase {
|
||||
'test_field' => ['test_field'],
|
||||
],
|
||||
];
|
||||
db_create_table('test_table', $table_specification);
|
||||
$this->schema->createTable('test_table', $table_specification);
|
||||
|
||||
// Tests for indexes are Database specific.
|
||||
$db_type = Database::getConnection()->databaseType();
|
||||
$db_type = $this->connection->databaseType();
|
||||
|
||||
// Test for existing primary and unique keys.
|
||||
switch ($db_type) {
|
||||
case 'pgsql':
|
||||
$primary_key_exists = Database::getConnection()->schema()->constraintExists('test_table', '__pkey');
|
||||
$unique_key_exists = Database::getConnection()->schema()->constraintExists('test_table', 'test_field' . '__key');
|
||||
$primary_key_exists = $this->schema->constraintExists('test_table', '__pkey');
|
||||
$unique_key_exists = $this->schema->constraintExists('test_table', 'test_field' . '__key');
|
||||
break;
|
||||
|
||||
case 'sqlite':
|
||||
// SQLite does not create a standalone index for primary keys.
|
||||
$primary_key_exists = TRUE;
|
||||
$unique_key_exists = Database::getConnection()->schema()->indexExists('test_table', 'test_field');
|
||||
$unique_key_exists = $this->schema->indexExists('test_table', 'test_field');
|
||||
break;
|
||||
|
||||
default:
|
||||
$primary_key_exists = Database::getConnection()->schema()->indexExists('test_table', 'PRIMARY');
|
||||
$unique_key_exists = Database::getConnection()->schema()->indexExists('test_table', 'test_field');
|
||||
$primary_key_exists = $this->schema->indexExists('test_table', 'PRIMARY');
|
||||
$unique_key_exists = $this->schema->indexExists('test_table', 'test_field');
|
||||
break;
|
||||
}
|
||||
$this->assertIdentical($primary_key_exists, TRUE, 'Primary key created.');
|
||||
$this->assertIdentical($unique_key_exists, TRUE, 'Unique key created.');
|
||||
|
||||
db_rename_table('test_table', 'test_table2');
|
||||
$this->schema->renameTable('test_table', 'test_table2');
|
||||
|
||||
// Test for renamed primary and unique keys.
|
||||
switch ($db_type) {
|
||||
case 'pgsql':
|
||||
$renamed_primary_key_exists = Database::getConnection()->schema()->constraintExists('test_table2', '__pkey');
|
||||
$renamed_unique_key_exists = Database::getConnection()->schema()->constraintExists('test_table2', 'test_field' . '__key');
|
||||
$renamed_primary_key_exists = $this->schema->constraintExists('test_table2', '__pkey');
|
||||
$renamed_unique_key_exists = $this->schema->constraintExists('test_table2', 'test_field' . '__key');
|
||||
break;
|
||||
|
||||
case 'sqlite':
|
||||
// SQLite does not create a standalone index for primary keys.
|
||||
$renamed_primary_key_exists = TRUE;
|
||||
$renamed_unique_key_exists = Database::getConnection()->schema()->indexExists('test_table2', 'test_field');
|
||||
$renamed_unique_key_exists = $this->schema->indexExists('test_table2', 'test_field');
|
||||
break;
|
||||
|
||||
default:
|
||||
$renamed_primary_key_exists = Database::getConnection()->schema()->indexExists('test_table2', 'PRIMARY');
|
||||
$renamed_unique_key_exists = Database::getConnection()->schema()->indexExists('test_table2', 'test_field');
|
||||
$renamed_primary_key_exists = $this->schema->indexExists('test_table2', 'PRIMARY');
|
||||
$renamed_unique_key_exists = $this->schema->indexExists('test_table2', 'test_field');
|
||||
break;
|
||||
}
|
||||
$this->assertIdentical($renamed_primary_key_exists, TRUE, 'Primary key was renamed.');
|
||||
@@ -231,8 +266,8 @@ class SchemaTest extends KernelTestBase {
|
||||
// For PostgreSQL check in addition that sequence was renamed.
|
||||
if ($db_type == 'pgsql') {
|
||||
// Get information about new table.
|
||||
$info = Database::getConnection()->schema()->queryTableInformation('test_table2');
|
||||
$sequence_name = Database::getConnection()->schema()->prefixNonTable('test_table2', 'id', 'seq');
|
||||
$info = $this->schema->queryTableInformation('test_table2');
|
||||
$sequence_name = $this->schema->prefixNonTable('test_table2', 'id', 'seq');
|
||||
$this->assertEqual($sequence_name, current($info->sequences), 'Sequence was renamed.');
|
||||
}
|
||||
|
||||
@@ -250,11 +285,11 @@ class SchemaTest extends KernelTestBase {
|
||||
],
|
||||
];
|
||||
try {
|
||||
db_create_table('test_timestamp', $table_specification);
|
||||
$this->schema->createTable('test_timestamp', $table_specification);
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
}
|
||||
$this->assertTrue(db_table_exists('test_timestamp'), 'Table with database specific datatype was created.');
|
||||
$this->assertTrue($this->schema->tableExists('test_timestamp'), 'Table with database specific datatype was created.');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -263,9 +298,10 @@ class SchemaTest extends KernelTestBase {
|
||||
* @see \Drupal\Core\Database\Driver\mysql\Schema::getNormalizedIndexes()
|
||||
*/
|
||||
public function testIndexLength() {
|
||||
if (Database::getConnection()->databaseType() != 'mysql') {
|
||||
return;
|
||||
if ($this->connection->databaseType() !== 'mysql') {
|
||||
$this->markTestSkipped("The '{$this->connection->databaseType()}' database type does not support setting column length for indexes.");
|
||||
}
|
||||
|
||||
$table_specification = [
|
||||
'fields' => [
|
||||
'id' => [
|
||||
@@ -312,16 +348,14 @@ class SchemaTest extends KernelTestBase {
|
||||
],
|
||||
],
|
||||
];
|
||||
db_create_table('test_table_index_length', $table_specification);
|
||||
|
||||
$schema_object = Database::getConnection()->schema();
|
||||
$this->schema->createTable('test_table_index_length', $table_specification);
|
||||
|
||||
// Ensure expected exception thrown when adding index with missing info.
|
||||
$expected_exception_message = "MySQL needs the 'test_field_text' field specification in order to normalize the 'test_regular' index";
|
||||
$missing_field_spec = $table_specification;
|
||||
unset($missing_field_spec['fields']['test_field_text']);
|
||||
try {
|
||||
$schema_object->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $missing_field_spec);
|
||||
$this->schema->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $missing_field_spec);
|
||||
$this->fail('SchemaException not thrown when adding index with missing information.');
|
||||
}
|
||||
catch (SchemaException $e) {
|
||||
@@ -329,14 +363,13 @@ class SchemaTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
// Add a separate index.
|
||||
$schema_object->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $table_specification);
|
||||
$this->schema->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $table_specification);
|
||||
$table_specification_with_new_index = $table_specification;
|
||||
$table_specification_with_new_index['indexes']['test_separate'] = [['test_field_text', 200]];
|
||||
|
||||
// Ensure that the exceptions of addIndex are thrown as expected.
|
||||
|
||||
try {
|
||||
$schema_object->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $table_specification);
|
||||
$this->schema->addIndex('test_table_index_length', 'test_separate', [['test_field_text', 200]], $table_specification);
|
||||
$this->fail('\Drupal\Core\Database\SchemaObjectExistsException exception missed.');
|
||||
}
|
||||
catch (SchemaObjectExistsException $e) {
|
||||
@@ -344,7 +377,7 @@ class SchemaTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
try {
|
||||
$schema_object->addIndex('test_table_non_existing', 'test_separate', [['test_field_text', 200]], $table_specification);
|
||||
$this->schema->addIndex('test_table_non_existing', 'test_separate', [['test_field_text', 200]], $table_specification);
|
||||
$this->fail('\Drupal\Core\Database\SchemaObjectDoesNotExistException exception missed.');
|
||||
}
|
||||
catch (SchemaObjectDoesNotExistException $e) {
|
||||
@@ -352,7 +385,7 @@ class SchemaTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
// Get index information.
|
||||
$results = db_query('SHOW INDEX FROM {test_table_index_length}');
|
||||
$results = $this->connection->query('SHOW INDEX FROM {test_table_index_length}');
|
||||
$expected_lengths = [
|
||||
'test_regular' => [
|
||||
'test_field_text' => 191,
|
||||
@@ -395,15 +428,16 @@ class SchemaTest extends KernelTestBase {
|
||||
/**
|
||||
* Tests inserting data into an existing table.
|
||||
*
|
||||
* @param $table
|
||||
* @param string $table
|
||||
* The database table to insert data into.
|
||||
*
|
||||
* @return
|
||||
* @return bool
|
||||
* TRUE if the insert succeeded, FALSE otherwise.
|
||||
*/
|
||||
public function tryInsert($table = 'test_table') {
|
||||
try {
|
||||
db_insert($table)
|
||||
$this->connection
|
||||
->insert($table)
|
||||
->fields(['id' => mt_rand(10, 20)])
|
||||
->execute();
|
||||
return TRUE;
|
||||
@@ -424,10 +458,10 @@ class SchemaTest extends KernelTestBase {
|
||||
* Optional column to test.
|
||||
*/
|
||||
public function checkSchemaComment($description, $table, $column = NULL) {
|
||||
if (method_exists(Database::getConnection()->schema(), 'getComment')) {
|
||||
$comment = Database::getConnection()->schema()->getComment($table, $column);
|
||||
if (method_exists($this->schema, 'getComment')) {
|
||||
$comment = $this->schema->getComment($table, $column);
|
||||
// The schema comment truncation for mysql is different.
|
||||
if (Database::getConnection()->databaseType() == 'mysql') {
|
||||
if ($this->connection->databaseType() === 'mysql') {
|
||||
$max_length = $column ? 255 : 60;
|
||||
$description = Unicode::truncate($description, $max_length, TRUE, TRUE);
|
||||
}
|
||||
@@ -445,7 +479,7 @@ class SchemaTest extends KernelTestBase {
|
||||
'fields' => ['serial_column' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE]],
|
||||
'primary key' => ['serial_column'],
|
||||
];
|
||||
db_create_table($table_name, $table_spec);
|
||||
$this->schema->createTable($table_name, $table_spec);
|
||||
|
||||
// Now set up columns for the other types.
|
||||
$types = ['int', 'float', 'numeric'];
|
||||
@@ -456,12 +490,12 @@ class SchemaTest extends KernelTestBase {
|
||||
}
|
||||
$column_name = $type . '_column';
|
||||
$table_spec['fields'][$column_name] = $column_spec;
|
||||
db_add_field($table_name, $column_name, $column_spec);
|
||||
$this->schema->addField($table_name, $column_name, $column_spec);
|
||||
}
|
||||
|
||||
// Finally, check each column and try to insert invalid values into them.
|
||||
foreach ($table_spec['fields'] as $column_name => $column_spec) {
|
||||
$this->assertTrue(db_field_exists($table_name, $column_name), format_string('Unsigned @type column was created.', ['@type' => $column_spec['type']]));
|
||||
$this->assertTrue($this->schema->fieldExists($table_name, $column_name), format_string('Unsigned @type column was created.', ['@type' => $column_spec['type']]));
|
||||
$this->assertFalse($this->tryUnsignedInsert($table_name, $column_name), format_string('Unsigned @type column rejected a negative value.', ['@type' => $column_spec['type']]));
|
||||
}
|
||||
}
|
||||
@@ -469,17 +503,18 @@ class SchemaTest extends KernelTestBase {
|
||||
/**
|
||||
* Tries to insert a negative value into columns defined as unsigned.
|
||||
*
|
||||
* @param $table_name
|
||||
* @param string $table_name
|
||||
* The table to insert.
|
||||
* @param $column_name
|
||||
* @param string $column_name
|
||||
* The column to insert.
|
||||
*
|
||||
* @return
|
||||
* @return bool
|
||||
* TRUE if the insert succeeded, FALSE otherwise.
|
||||
*/
|
||||
public function tryUnsignedInsert($table_name, $column_name) {
|
||||
try {
|
||||
db_insert($table_name)
|
||||
$this->connection
|
||||
->insert($table_name)
|
||||
->fields([$column_name => -1])
|
||||
->execute();
|
||||
return TRUE;
|
||||
@@ -490,9 +525,9 @@ class SchemaTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests adding columns to an existing table.
|
||||
* Tests adding columns to an existing table with default and initial value.
|
||||
*/
|
||||
public function testSchemaAddField() {
|
||||
public function testSchemaAddFieldDefaultInitial() {
|
||||
// Test varchar types.
|
||||
foreach ([1, 32, 128, 256, 512] as $length) {
|
||||
$base_field_spec = [
|
||||
@@ -528,6 +563,11 @@ class SchemaTest extends KernelTestBase {
|
||||
['not null' => TRUE, 'initial' => 1],
|
||||
['not null' => TRUE, 'initial' => 1, 'default' => 7],
|
||||
['not null' => TRUE, 'initial_from_field' => 'serial_column'],
|
||||
[
|
||||
'not null' => TRUE,
|
||||
'initial_from_field' => 'test_nullable_field',
|
||||
'initial' => 100,
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($variations as $variation) {
|
||||
@@ -581,51 +621,61 @@ class SchemaTest extends KernelTestBase {
|
||||
$table_spec = [
|
||||
'fields' => [
|
||||
'serial_column' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE],
|
||||
'test_nullable_field' => ['type' => 'int', 'not null' => FALSE],
|
||||
'test_field' => $field_spec,
|
||||
],
|
||||
'primary key' => ['serial_column'],
|
||||
];
|
||||
db_create_table($table_name, $table_spec);
|
||||
$this->schema->createTable($table_name, $table_spec);
|
||||
$this->pass(format_string('Table %table created.', ['%table' => $table_name]));
|
||||
|
||||
// Check the characteristics of the field.
|
||||
$this->assertFieldCharacteristics($table_name, 'test_field', $field_spec);
|
||||
|
||||
// Clean-up.
|
||||
db_drop_table($table_name);
|
||||
$this->schema->dropTable($table_name);
|
||||
|
||||
// Try adding a field to an existing table.
|
||||
$table_name = 'test_table_' . ($this->counter++);
|
||||
$table_spec = [
|
||||
'fields' => [
|
||||
'serial_column' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE],
|
||||
'test_nullable_field' => ['type' => 'int', 'not null' => FALSE],
|
||||
],
|
||||
'primary key' => ['serial_column'],
|
||||
];
|
||||
db_create_table($table_name, $table_spec);
|
||||
$this->schema->createTable($table_name, $table_spec);
|
||||
$this->pass(format_string('Table %table created.', ['%table' => $table_name]));
|
||||
|
||||
// Insert some rows to the table to test the handling of initial values.
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
db_insert($table_name)
|
||||
$this->connection
|
||||
->insert($table_name)
|
||||
->useDefaults(['serial_column'])
|
||||
->fields(['test_nullable_field' => 100])
|
||||
->execute();
|
||||
}
|
||||
|
||||
db_add_field($table_name, 'test_field', $field_spec);
|
||||
// Add another row with no value for the 'test_nullable_field' column.
|
||||
$this->connection
|
||||
->insert($table_name)
|
||||
->useDefaults(['serial_column'])
|
||||
->execute();
|
||||
|
||||
$this->schema->addField($table_name, 'test_field', $field_spec);
|
||||
$this->pass(format_string('Column %column created.', ['%column' => 'test_field']));
|
||||
|
||||
// Check the characteristics of the field.
|
||||
$this->assertFieldCharacteristics($table_name, 'test_field', $field_spec);
|
||||
|
||||
// Clean-up.
|
||||
db_drop_field($table_name, 'test_field');
|
||||
$this->schema->dropField($table_name, 'test_field');
|
||||
|
||||
// Add back the field and then try to delete a field which is also a primary
|
||||
// key.
|
||||
db_add_field($table_name, 'test_field', $field_spec);
|
||||
db_drop_field($table_name, 'serial_column');
|
||||
db_drop_table($table_name);
|
||||
$this->schema->addField($table_name, 'test_field', $field_spec);
|
||||
$this->schema->dropField($table_name, 'serial_column');
|
||||
$this->schema->dropTable($table_name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -635,7 +685,8 @@ class SchemaTest extends KernelTestBase {
|
||||
// Check that the initial value has been registered.
|
||||
if (isset($field_spec['initial'])) {
|
||||
// There should be no row with a value different then $field_spec['initial'].
|
||||
$count = db_select($table_name)
|
||||
$count = $this->connection
|
||||
->select($table_name)
|
||||
->fields($table_name, ['serial_column'])
|
||||
->condition($field_name, $field_spec['initial'], '<>')
|
||||
->countQuery()
|
||||
@@ -645,10 +696,11 @@ class SchemaTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
// Check that the initial value from another field has been registered.
|
||||
if (isset($field_spec['initial_from_field'])) {
|
||||
if (isset($field_spec['initial_from_field']) && !isset($field_spec['initial'])) {
|
||||
// There should be no row with a value different than
|
||||
// $field_spec['initial_from_field'].
|
||||
$count = db_select($table_name)
|
||||
$count = $this->connection
|
||||
->select($table_name)
|
||||
->fields($table_name, ['serial_column'])
|
||||
->where($table_name . '.' . $field_spec['initial_from_field'] . ' <> ' . $table_name . '.' . $field_name)
|
||||
->countQuery()
|
||||
@@ -656,14 +708,27 @@ class SchemaTest extends KernelTestBase {
|
||||
->fetchField();
|
||||
$this->assertEqual($count, 0, 'Initial values from another field filled out.');
|
||||
}
|
||||
elseif (isset($field_spec['initial_from_field']) && isset($field_spec['initial'])) {
|
||||
// There should be no row with a value different than '100'.
|
||||
$count = $this->connection
|
||||
->select($table_name)
|
||||
->fields($table_name, ['serial_column'])
|
||||
->condition($field_name, 100, '<>')
|
||||
->countQuery()
|
||||
->execute()
|
||||
->fetchField();
|
||||
$this->assertEqual($count, 0, 'Initial values from another field or a default value filled out.');
|
||||
}
|
||||
|
||||
// Check that the default value has been registered.
|
||||
if (isset($field_spec['default'])) {
|
||||
// Try inserting a row, and check the resulting value of the new column.
|
||||
$id = db_insert($table_name)
|
||||
$id = $this->connection
|
||||
->insert($table_name)
|
||||
->useDefaults(['serial_column'])
|
||||
->execute();
|
||||
$field_value = db_select($table_name)
|
||||
$field_value = $this->connection
|
||||
->select($table_name)
|
||||
->fields($table_name, [$field_name])
|
||||
->condition('serial_column', $id)
|
||||
->execute()
|
||||
@@ -673,9 +738,171 @@ class SchemaTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests changing columns between types.
|
||||
* Tests various schema changes' effect on the table's primary key.
|
||||
*
|
||||
* @param array $initial_primary_key
|
||||
* The initial primary key of the test table.
|
||||
* @param array $renamed_primary_key
|
||||
* The primary key of the test table after renaming the test field.
|
||||
*
|
||||
* @dataProvider providerTestSchemaCreateTablePrimaryKey
|
||||
*
|
||||
* @covers ::addField
|
||||
* @covers ::changeField
|
||||
* @covers ::dropField
|
||||
* @covers ::findPrimaryKeyColumns
|
||||
*/
|
||||
public function testSchemaChangeField() {
|
||||
public function testSchemaChangePrimaryKey(array $initial_primary_key, array $renamed_primary_key) {
|
||||
$find_primary_key_columns = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
|
||||
$find_primary_key_columns->setAccessible(TRUE);
|
||||
|
||||
// Test making the field the primary key of the table upon creation.
|
||||
$table_name = 'test_table';
|
||||
$table_spec = [
|
||||
'fields' => [
|
||||
'test_field' => ['type' => 'int', 'not null' => TRUE],
|
||||
'other_test_field' => ['type' => 'int', 'not null' => TRUE],
|
||||
],
|
||||
'primary key' => $initial_primary_key,
|
||||
];
|
||||
$this->schema->createTable($table_name, $table_spec);
|
||||
$this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
|
||||
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
|
||||
|
||||
// Change the field type and make sure the primary key stays in place.
|
||||
$this->schema->changeField($table_name, 'test_field', 'test_field', ['type' => 'varchar', 'length' => 32, 'not null' => TRUE]);
|
||||
$this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
|
||||
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
|
||||
|
||||
// Add some data and change the field type back, to make sure that changing
|
||||
// the type leaves the primary key in place even with existing data.
|
||||
$this->connection
|
||||
->insert($table_name)
|
||||
->fields(['test_field' => 1, 'other_test_field' => 2])
|
||||
->execute();
|
||||
$this->schema->changeField($table_name, 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE]);
|
||||
$this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
|
||||
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
|
||||
|
||||
// Make sure that adding the primary key can be done as part of changing
|
||||
// a field, as well.
|
||||
$this->schema->dropPrimaryKey($table_name);
|
||||
$this->assertEquals([], $find_primary_key_columns->invoke($this->schema, $table_name));
|
||||
$this->schema->changeField($table_name, 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE], ['primary key' => $initial_primary_key]);
|
||||
$this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
|
||||
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
|
||||
|
||||
// Rename the field and make sure the primary key was updated.
|
||||
$this->schema->changeField($table_name, 'test_field', 'test_field_renamed', ['type' => 'int', 'not null' => TRUE]);
|
||||
$this->assertTrue($this->schema->fieldExists($table_name, 'test_field_renamed'));
|
||||
$this->assertEquals($renamed_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
|
||||
|
||||
// Drop the field and make sure the primary key was dropped, as well.
|
||||
$this->schema->dropField($table_name, 'test_field_renamed');
|
||||
$this->assertFalse($this->schema->fieldExists($table_name, 'test_field_renamed'));
|
||||
$this->assertEquals([], $find_primary_key_columns->invoke($this->schema, $table_name));
|
||||
|
||||
// Add the field again and make sure adding the primary key can be done at
|
||||
// the same time.
|
||||
$this->schema->addField($table_name, 'test_field', ['type' => 'int', 'default' => 0, 'not null' => TRUE], ['primary key' => $initial_primary_key]);
|
||||
$this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
|
||||
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
|
||||
|
||||
// Drop the field again and explicitly add a primary key.
|
||||
$this->schema->dropField($table_name, 'test_field');
|
||||
$this->schema->addPrimaryKey($table_name, ['other_test_field']);
|
||||
$this->assertFalse($this->schema->fieldExists($table_name, 'test_field'));
|
||||
$this->assertEquals(['other_test_field'], $find_primary_key_columns->invoke($this->schema, $table_name));
|
||||
|
||||
// Test that adding a field with a primary key will work even with a
|
||||
// pre-existing primary key.
|
||||
$this->schema->addField($table_name, 'test_field', ['type' => 'int', 'default' => 0, 'not null' => TRUE], ['primary key' => $initial_primary_key]);
|
||||
$this->assertTrue($this->schema->fieldExists($table_name, 'test_field'));
|
||||
$this->assertEquals($initial_primary_key, $find_primary_key_columns->invoke($this->schema, $table_name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test cases for SchemaTest::testSchemaCreateTablePrimaryKey().
|
||||
*
|
||||
* @return array
|
||||
* An array of test cases for SchemaTest::testSchemaCreateTablePrimaryKey().
|
||||
*/
|
||||
public function providerTestSchemaCreateTablePrimaryKey() {
|
||||
$tests = [];
|
||||
|
||||
$tests['simple_primary_key'] = [
|
||||
'initial_primary_key' => ['test_field'],
|
||||
'renamed_primary_key' => ['test_field_renamed'],
|
||||
];
|
||||
$tests['composite_primary_key'] = [
|
||||
'initial_primary_key' => ['test_field', 'other_test_field'],
|
||||
'renamed_primary_key' => ['test_field_renamed', 'other_test_field'],
|
||||
];
|
||||
$tests['composite_primary_key_different_order'] = [
|
||||
'initial_primary_key' => ['other_test_field', 'test_field'],
|
||||
'renamed_primary_key' => ['other_test_field', 'test_field_renamed'],
|
||||
];
|
||||
|
||||
return $tests;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests an invalid field specification as a primary key on table creation.
|
||||
*/
|
||||
public function testInvalidPrimaryKeyOnTableCreation() {
|
||||
// Test making an invalid field the primary key of the table upon creation.
|
||||
$table_name = 'test_table';
|
||||
$table_spec = [
|
||||
'fields' => [
|
||||
'test_field' => ['type' => 'int'],
|
||||
],
|
||||
'primary key' => ['test_field'],
|
||||
];
|
||||
$this->setExpectedException(SchemaException::class, "The 'test_field' field specification does not define 'not null' as TRUE.");
|
||||
$this->schema->createTable($table_name, $table_spec);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests adding an invalid field specification as a primary key.
|
||||
*/
|
||||
public function testInvalidPrimaryKeyAddition() {
|
||||
// Test adding a new invalid field to the primary key.
|
||||
$table_name = 'test_table';
|
||||
$table_spec = [
|
||||
'fields' => [
|
||||
'test_field' => ['type' => 'int', 'not null' => TRUE],
|
||||
],
|
||||
'primary key' => ['test_field'],
|
||||
];
|
||||
$this->schema->createTable($table_name, $table_spec);
|
||||
|
||||
$this->setExpectedException(SchemaException::class, "The 'new_test_field' field specification does not define 'not null' as TRUE.");
|
||||
$this->schema->addField($table_name, 'new_test_field', ['type' => 'int'], ['primary key' => ['test_field', 'new_test_field']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests changing the primary key with an invalid field specification.
|
||||
*/
|
||||
public function testInvalidPrimaryKeyChange() {
|
||||
// Test adding a new invalid field to the primary key.
|
||||
$table_name = 'test_table';
|
||||
$table_spec = [
|
||||
'fields' => [
|
||||
'test_field' => ['type' => 'int', 'not null' => TRUE],
|
||||
],
|
||||
'primary key' => ['test_field'],
|
||||
];
|
||||
$this->schema->createTable($table_name, $table_spec);
|
||||
|
||||
$this->setExpectedException(SchemaException::class, "The 'changed_test_field' field specification does not define 'not null' as TRUE.");
|
||||
$this->schema->dropPrimaryKey($table_name);
|
||||
$this->schema->changeField($table_name, 'test_field', 'changed_test_field', ['type' => 'int'], ['primary key' => ['changed_test_field']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests changing columns between types with default and initial values.
|
||||
*/
|
||||
public function testSchemaChangeFieldDefaultInitial() {
|
||||
$field_specs = [
|
||||
['type' => 'int', 'size' => 'normal', 'not null' => FALSE],
|
||||
['type' => 'int', 'size' => 'normal', 'not null' => TRUE, 'initial' => 1, 'default' => 17],
|
||||
@@ -734,26 +961,28 @@ class SchemaTest extends KernelTestBase {
|
||||
],
|
||||
'primary key' => ['serial_column'],
|
||||
];
|
||||
db_create_table($table_name, $table_spec);
|
||||
$this->schema->createTable($table_name, $table_spec);
|
||||
$this->pass(format_string('Table %table created.', ['%table' => $table_name]));
|
||||
|
||||
// Check the characteristics of the field.
|
||||
$this->assertFieldCharacteristics($table_name, 'test_field', $old_spec);
|
||||
|
||||
// Remove inserted rows.
|
||||
db_truncate($table_name)->execute();
|
||||
$this->connection->truncate($table_name)->execute();
|
||||
|
||||
if ($test_data) {
|
||||
$id = db_insert($table_name)
|
||||
$id = $this->connection
|
||||
->insert($table_name)
|
||||
->fields(['test_field'], [$test_data])
|
||||
->execute();
|
||||
}
|
||||
|
||||
// Change the field.
|
||||
db_change_field($table_name, 'test_field', 'test_field', $new_spec);
|
||||
$this->schema->changeField($table_name, 'test_field', 'test_field', $new_spec);
|
||||
|
||||
if ($test_data) {
|
||||
$field_value = db_select($table_name)
|
||||
$field_value = $this->connection
|
||||
->select($table_name)
|
||||
->fields($table_name, ['test_field'])
|
||||
->condition('serial_column', $id)
|
||||
->execute()
|
||||
@@ -765,7 +994,142 @@ class SchemaTest extends KernelTestBase {
|
||||
$this->assertFieldCharacteristics($table_name, 'test_field', $new_spec);
|
||||
|
||||
// Clean-up.
|
||||
db_drop_table($table_name);
|
||||
$this->schema->dropTable($table_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::findPrimaryKeyColumns
|
||||
*/
|
||||
public function testFindPrimaryKeyColumns() {
|
||||
$method = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
|
||||
$method->setAccessible(TRUE);
|
||||
|
||||
// Test with single column primary key.
|
||||
$this->schema->createTable('table_with_pk_0', [
|
||||
'description' => 'Table with primary key.',
|
||||
'fields' => [
|
||||
'id' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
'test_field' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
],
|
||||
'primary key' => ['id'],
|
||||
]);
|
||||
$this->assertSame(['id'], $method->invoke($this->schema, 'table_with_pk_0'));
|
||||
|
||||
// Test with multiple column primary key.
|
||||
$this->schema->createTable('table_with_pk_1', [
|
||||
'description' => 'Table with primary key with multiple columns.',
|
||||
'fields' => [
|
||||
'id0' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
'id1' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
'test_field' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
],
|
||||
'primary key' => ['id0', 'id1'],
|
||||
]);
|
||||
$this->assertSame(['id0', 'id1'], $method->invoke($this->schema, 'table_with_pk_1'));
|
||||
|
||||
// Test with multiple column primary key and not being the first column of
|
||||
// the table definition.
|
||||
$this->schema->createTable('table_with_pk_2', [
|
||||
'description' => 'Table with primary key with multiple columns at the end and in reverted sequence.',
|
||||
'fields' => [
|
||||
'test_field_1' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
'test_field_2' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
'id3' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
'id4' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
],
|
||||
'primary key' => ['id4', 'id3'],
|
||||
]);
|
||||
$this->assertSame(['id4', 'id3'], $method->invoke($this->schema, 'table_with_pk_2'));
|
||||
|
||||
// Test with multiple column primary key in a different order. For the
|
||||
// PostgreSQL and the SQLite drivers is sorting used to get the primary key
|
||||
// columns in the right order.
|
||||
$this->schema->createTable('table_with_pk_3', [
|
||||
'description' => 'Table with primary key with multiple columns at the end and in reverted sequence.',
|
||||
'fields' => [
|
||||
'test_field_1' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
'test_field_2' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
'id3' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
'id4' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
],
|
||||
'primary key' => ['id3', 'test_field_2', 'id4'],
|
||||
]);
|
||||
$this->assertSame(['id3', 'test_field_2', 'id4'], $method->invoke($this->schema, 'table_with_pk_3'));
|
||||
|
||||
// Test with table without a primary key.
|
||||
$this->schema->createTable('table_without_pk_1', [
|
||||
'description' => 'Table without primary key.',
|
||||
'fields' => [
|
||||
'id' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
'test_field' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
],
|
||||
]);
|
||||
$this->assertSame([], $method->invoke($this->schema, 'table_without_pk_1'));
|
||||
|
||||
// Test with table with an empty primary key.
|
||||
$this->schema->createTable('table_without_pk_2', [
|
||||
'description' => 'Table without primary key.',
|
||||
'fields' => [
|
||||
'id' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
'test_field' => [
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
],
|
||||
],
|
||||
'primary key' => [],
|
||||
]);
|
||||
$this->assertSame([], $method->invoke($this->schema, 'table_without_pk_2'));
|
||||
|
||||
// Test with non existing table.
|
||||
$this->assertFalse($method->invoke($this->schema, 'non_existing_table'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -774,7 +1138,6 @@ class SchemaTest extends KernelTestBase {
|
||||
public function testFindTables() {
|
||||
// We will be testing with three tables, two of them using the default
|
||||
// prefix and the third one with an individually specified prefix.
|
||||
|
||||
// Set up a new connection with different connection info.
|
||||
$connection_info = Database::getConnectionInfo();
|
||||
|
||||
@@ -782,8 +1145,8 @@ class SchemaTest extends KernelTestBase {
|
||||
$new_connection_info = $connection_info['default'];
|
||||
$new_connection_info['prefix']['test_2_table'] = $new_connection_info['prefix']['default'] . '_shared_';
|
||||
Database::addConnectionInfo('test', 'default', $new_connection_info);
|
||||
|
||||
Database::setActiveConnection('test');
|
||||
$test_schema = Database::getConnection()->schema();
|
||||
|
||||
// Create the tables.
|
||||
$table_specification = [
|
||||
@@ -795,12 +1158,12 @@ class SchemaTest extends KernelTestBase {
|
||||
],
|
||||
],
|
||||
];
|
||||
Database::getConnection()->schema()->createTable('test_1_table', $table_specification);
|
||||
Database::getConnection()->schema()->createTable('test_2_table', $table_specification);
|
||||
Database::getConnection()->schema()->createTable('the_third_table', $table_specification);
|
||||
$test_schema->createTable('test_1_table', $table_specification);
|
||||
$test_schema->createTable('test_2_table', $table_specification);
|
||||
$test_schema->createTable('the_third_table', $table_specification);
|
||||
|
||||
// Check the "all tables" syntax.
|
||||
$tables = Database::getConnection()->schema()->findTables('%');
|
||||
$tables = $test_schema->findTables('%');
|
||||
sort($tables);
|
||||
$expected = [
|
||||
// The 'config' table is added by
|
||||
@@ -814,7 +1177,7 @@ class SchemaTest extends KernelTestBase {
|
||||
$this->assertEqual($tables, $expected, 'All tables were found.');
|
||||
|
||||
// Check the restrictive syntax.
|
||||
$tables = Database::getConnection()->schema()->findTables('test_%');
|
||||
$tables = $test_schema->findTables('test_%');
|
||||
sort($tables);
|
||||
$expected = [
|
||||
'test_1_table',
|
||||
@@ -826,46 +1189,4 @@ class SchemaTest extends KernelTestBase {
|
||||
Database::setActiveConnection('default');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the primary keys of a table.
|
||||
*
|
||||
* @param string $table_name
|
||||
* The name of the table to check.
|
||||
* @param array $primary_key
|
||||
* The expected key column specifier for a table's primary key.
|
||||
*/
|
||||
protected function assertPrimaryKeyColumns($table_name, array $primary_key = []) {
|
||||
$db_type = Database::getConnection()->databaseType();
|
||||
|
||||
switch ($db_type) {
|
||||
case 'mysql':
|
||||
$result = Database::getConnection()->query("SHOW KEYS FROM {" . $table_name . "} WHERE Key_name = 'PRIMARY'")->fetchAllAssoc('Column_name');
|
||||
$this->assertSame($primary_key, array_keys($result));
|
||||
|
||||
break;
|
||||
case 'pgsql':
|
||||
$result = Database::getConnection()->query("SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type
|
||||
FROM pg_index i
|
||||
JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)
|
||||
WHERE i.indrelid = '{" . $table_name . "}'::regclass AND i.indisprimary")
|
||||
->fetchAllAssoc('attname');
|
||||
$this->assertSame($primary_key, array_keys($result));
|
||||
|
||||
break;
|
||||
case 'sqlite':
|
||||
// For SQLite we need access to the protected
|
||||
// \Drupal\Core\Database\Driver\sqlite\Schema::introspectSchema() method
|
||||
// because we have no other way of getting the table prefixes needed for
|
||||
// running a straight PRAGMA query.
|
||||
$schema_object = Database::getConnection()->schema();
|
||||
$reflection = new \ReflectionMethod($schema_object, 'introspectSchema');
|
||||
$reflection->setAccessible(TRUE);
|
||||
|
||||
$table_info = $reflection->invoke($schema_object, $table_name);
|
||||
$this->assertSame($primary_key, $table_info['primary key']);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -245,7 +245,6 @@ class SelectComplexTest extends DatabaseTestBase {
|
||||
$this->assertEqual($count, 4, 'Counted the correct number of records.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests that countQuery properly removes fields and expressions.
|
||||
*/
|
||||
|
||||
@@ -54,7 +54,7 @@ class SelectTest extends DatabaseTestBase {
|
||||
$records = $result->fetchAll();
|
||||
|
||||
$query = (string) $query;
|
||||
$expected = "/* Testing query comments * / SELECT nid FROM {node}. -- */ SELECT test.name AS name, test.age AS age\nFROM \n{test} test";
|
||||
$expected = "/* Testing query comments * / SELECT nid FROM {node}. -- */ SELECT test.name AS name, test.age AS age\nFROM\n{test} test";
|
||||
|
||||
$this->assertEqual(count($records), 4, 'Returned the correct number of rows.');
|
||||
$this->assertNotIdentical(FALSE, strpos($query, $expected), 'The flattened query contains the sanitised comment string.');
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Drupal\KernelTests\Core\Database;
|
||||
* @group Database
|
||||
*/
|
||||
class SerializeQueryTest extends DatabaseTestBase {
|
||||
|
||||
/**
|
||||
* Confirms that a query can be serialized and unserialized.
|
||||
*/
|
||||
|
||||
@@ -53,4 +53,40 @@ class UpsertTest extends DatabaseTestBase {
|
||||
$this->assertEqual($person->name, 'Meredith', 'Name was not changed.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that we can upsert records with a special named column.
|
||||
*/
|
||||
public function testSpecialColumnUpsert() {
|
||||
$num_records_before = $this->connection->query('SELECT COUNT(*) FROM {test_special_columns}')->fetchField();
|
||||
$upsert = $this->connection->upsert('test_special_columns')
|
||||
->key('id')
|
||||
->fields(['id', 'offset', 'function']);
|
||||
|
||||
// Add a new row.
|
||||
$upsert->values([
|
||||
'id' => 2,
|
||||
'offset' => 'Offset 2',
|
||||
'function' => 'Function 2',
|
||||
]);
|
||||
|
||||
// Update an existing row.
|
||||
$upsert->values([
|
||||
'id' => 1,
|
||||
'offset' => 'Offset 1 updated',
|
||||
'function' => 'Function 1 updated',
|
||||
]);
|
||||
|
||||
$upsert->execute();
|
||||
$num_records_after = $this->connection->query('SELECT COUNT(*) FROM {test_special_columns}')->fetchField();
|
||||
$this->assertEquals($num_records_before + 1, $num_records_after, 'Rows were inserted and updated properly.');
|
||||
|
||||
$record = $this->connection->query('SELECT * FROM {test_special_columns} WHERE id = :id', [':id' => 1])->fetch();
|
||||
$this->assertEquals($record->offset, 'Offset 1 updated');
|
||||
$this->assertEquals($record->function, 'Function 1 updated');
|
||||
|
||||
$record = $this->connection->query('SELECT * FROM {test_special_columns} WHERE id = :id', [':id' => 2])->fetch();
|
||||
$this->assertEquals($record->offset, 'Offset 2');
|
||||
$this->assertEquals($record->function, 'Function 2');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\Core\Config\Schema\Mapping;
|
||||
use Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter;
|
||||
use Drupal\Core\TypedData\Plugin\DataType\BooleanData;
|
||||
use Drupal\Core\TypedData\Plugin\DataType\IntegerData;
|
||||
use Drupal\Core\TypedData\Plugin\DataType\StringData;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests entity adapter for configuration entities.
|
||||
*
|
||||
* @see \Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter
|
||||
*
|
||||
* @group Entity
|
||||
*
|
||||
* @coversDefaultClass \Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter
|
||||
*/
|
||||
class ConfigEntityAdapterTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['config_test'];
|
||||
|
||||
/**
|
||||
* The config entity.
|
||||
*
|
||||
* @var \Drupal\config_test\Entity\ConfigTest
|
||||
*/
|
||||
protected $entity;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(static::$modules);
|
||||
|
||||
// ConfigTest::create doesn't work with the following exception:
|
||||
// "Multiple entity types found for Drupal\config_test\Entity\ConfigTest."
|
||||
$this->entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
|
||||
'id' => 'system',
|
||||
'label' => 'foobar',
|
||||
'weight' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Drupal\Core\Entity\Plugin\DataType\Deriver\EntityDeriver::getDerivativeDefinitions
|
||||
*/
|
||||
public function testEntityDeriver() {
|
||||
$definition = \Drupal::typedDataManager()->getDefinition('entity:config_test');
|
||||
$this->assertEquals(ConfigEntityAdapter::class, $definition['class']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::validate
|
||||
*/
|
||||
public function testValidate() {
|
||||
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
|
||||
$violations = $adapter->validate();
|
||||
$this->assertEmpty($violations);
|
||||
$this->entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
|
||||
'id' => 'system',
|
||||
'label' => 'foobar',
|
||||
// Set weight to be a string which should not validate.
|
||||
'weight' => 'very heavy',
|
||||
]);
|
||||
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
|
||||
$violations = $adapter->validate();
|
||||
$this->assertCount(1, $violations);
|
||||
$violation = $violations->get(0);
|
||||
$this->assertEquals('This value should be of the correct primitive type.', $violation->getMessage());
|
||||
$this->assertEquals('weight', $violation->getPropertyPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getProperties
|
||||
*/
|
||||
public function testGetProperties() {
|
||||
$expected_properties = [
|
||||
'uuid' => StringData::class,
|
||||
'langcode' => StringData::class,
|
||||
'status' => BooleanData::class,
|
||||
'dependencies' => Mapping::class,
|
||||
'id' => StringData::class,
|
||||
'label' => StringData::class,
|
||||
'weight' => IntegerData::class,
|
||||
'style' => StringData::class,
|
||||
'size' => StringData::class,
|
||||
'size_value' => StringData::class,
|
||||
'protected_property' => StringData::class,
|
||||
];
|
||||
$properties = ConfigEntityAdapter::createFromEntity($this->entity)->getProperties();
|
||||
$keys = [];
|
||||
foreach ($properties as $key => $property) {
|
||||
$keys[] = $key;
|
||||
$this->assertInstanceOf($expected_properties[$key], $property);
|
||||
}
|
||||
$this->assertSame(array_keys($expected_properties), $keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getValue
|
||||
*/
|
||||
public function testGetValue() {
|
||||
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
|
||||
$this->assertEquals($this->entity->weight, $adapter->get('weight')->getValue());
|
||||
$this->assertEquals($this->entity->id(), $adapter->get('id')->getValue());
|
||||
$this->assertEquals($this->entity->label, $adapter->get('label')->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::set
|
||||
*/
|
||||
public function testSet() {
|
||||
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
|
||||
// Get the value via typed data to ensure that the typed representation is
|
||||
// updated correctly when the value is set.
|
||||
$this->assertEquals(1, $adapter->get('weight')->getValue());
|
||||
|
||||
$return = $adapter->set('weight', 2);
|
||||
$this->assertSame($adapter, $return);
|
||||
$this->assertEquals(2, $this->entity->weight);
|
||||
// Ensure the typed data is updated via the set too.
|
||||
$this->assertEquals(2, $adapter->get('weight')->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getString
|
||||
*/
|
||||
public function testGetString() {
|
||||
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
|
||||
$this->assertEquals('foobar', $adapter->getString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::applyDefaultValue
|
||||
*/
|
||||
public function testApplyDefaultValue() {
|
||||
$this->setExpectedException(\BadMethodCallException::class, 'Method not supported');
|
||||
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
|
||||
$adapter->applyDefaultValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getIterator
|
||||
*/
|
||||
public function testGetIterator() {
|
||||
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
|
||||
$iterator = $adapter->getIterator();
|
||||
$fields = iterator_to_array($iterator);
|
||||
$expected_fields = [
|
||||
'uuid',
|
||||
'langcode',
|
||||
'status',
|
||||
'dependencies',
|
||||
'id',
|
||||
'label',
|
||||
'weight',
|
||||
'style',
|
||||
'size',
|
||||
'size_value',
|
||||
'protected_property',
|
||||
];
|
||||
$this->assertEquals($expected_fields, array_keys($fields));
|
||||
$this->assertEquals($this->entity->id(), $fields['id']->getValue());
|
||||
|
||||
$adapter->setValue(NULL);
|
||||
$this->assertEquals(new \ArrayIterator([]), $adapter->getIterator());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,10 +31,17 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
/**
|
||||
* The query factory used to construct all queries in the test.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\Query\QueryFactory
|
||||
* @var \Drupal\Core\Config\Entity\Query\QueryFactory
|
||||
*/
|
||||
protected $factory;
|
||||
|
||||
/**
|
||||
* The entity storage used for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $entityStorage;
|
||||
|
||||
/**
|
||||
* Stores all config entities created for the test.
|
||||
*
|
||||
@@ -46,7 +53,7 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
parent::setUp();
|
||||
|
||||
$this->entities = [];
|
||||
$this->factory = $this->container->get('entity.query');
|
||||
$this->entityStorage = $this->container->get('entity_type.manager')->getStorage('config_query_test');
|
||||
|
||||
// These two are here to make sure that matchArray needs to go over several
|
||||
// non-matches on every levels.
|
||||
@@ -114,82 +121,82 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
*/
|
||||
public function testConfigEntityQuery() {
|
||||
// Run a test without any condition.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->execute();
|
||||
$this->assertResults(['1', '2', '3', '4', '5']);
|
||||
// No conditions, OR.
|
||||
$this->queryResults = $this->factory->get('config_query_test', 'OR')
|
||||
$this->queryResults = $this->entityStorage->getQuery('OR')
|
||||
->execute();
|
||||
$this->assertResults(['1', '2', '3', '4', '5']);
|
||||
|
||||
// Filter by ID with equality.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', '3')
|
||||
->execute();
|
||||
$this->assertResults(['3']);
|
||||
|
||||
// Filter by label with a known prefix.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('label', 'test_prefix', 'STARTS_WITH')
|
||||
->execute();
|
||||
$this->assertResults(['3']);
|
||||
|
||||
// Filter by label with a known suffix.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('label', 'test_suffix', 'ENDS_WITH')
|
||||
->execute();
|
||||
$this->assertResults(['4']);
|
||||
|
||||
// Filter by label with a known containing word.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('label', 'test_contains', 'CONTAINS')
|
||||
->execute();
|
||||
$this->assertResults(['5']);
|
||||
|
||||
// Filter by ID with the IN operator.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', ['2', '3'], 'IN')
|
||||
->execute();
|
||||
$this->assertResults(['2', '3']);
|
||||
|
||||
// Filter by ID with the implicit IN operator.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', ['2', '3'])
|
||||
->execute();
|
||||
$this->assertResults(['2', '3']);
|
||||
|
||||
// Filter by ID with the > operator.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', '3', '>')
|
||||
->execute();
|
||||
$this->assertResults(['4', '5']);
|
||||
|
||||
// Filter by ID with the >= operator.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', '3', '>=')
|
||||
->execute();
|
||||
$this->assertResults(['3', '4', '5']);
|
||||
|
||||
// Filter by ID with the <> operator.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', '3', '<>')
|
||||
->execute();
|
||||
$this->assertResults(['1', '2', '4', '5']);
|
||||
|
||||
// Filter by ID with the < operator.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', '3', '<')
|
||||
->execute();
|
||||
$this->assertResults(['1', '2']);
|
||||
|
||||
// Filter by ID with the <= operator.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', '3', '<=')
|
||||
->execute();
|
||||
$this->assertResults(['1', '2', '3']);
|
||||
|
||||
// Filter by two conditions on the same field.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('label', 'test_pref', 'STARTS_WITH')
|
||||
->condition('label', 'test_prefix', 'STARTS_WITH')
|
||||
->execute();
|
||||
@@ -197,7 +204,7 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
|
||||
// Filter by two conditions on different fields. The first query matches for
|
||||
// a different ID, so the result is empty.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('label', 'test_prefix', 'STARTS_WITH')
|
||||
->condition('id', '5')
|
||||
->execute();
|
||||
@@ -205,7 +212,7 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
|
||||
// Filter by two different conditions on different fields. This time the
|
||||
// first condition matches on one item, but the second one does as well.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('label', 'test_prefix', 'STARTS_WITH')
|
||||
->condition('id', '3')
|
||||
->execute();
|
||||
@@ -214,7 +221,7 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
// Filter by two different conditions, of which the first one matches for
|
||||
// every entry, the second one as well, but just the third one filters so
|
||||
// that just two are left.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', '1', '>=')
|
||||
->condition('number', 10, '>=')
|
||||
->condition('number', 50, '>=')
|
||||
@@ -222,30 +229,30 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
$this->assertResults(['3', '5']);
|
||||
|
||||
// Filter with an OR condition group.
|
||||
$this->queryResults = $this->factory->get('config_query_test', 'OR')
|
||||
$this->queryResults = $this->entityStorage->getQuery('OR')
|
||||
->condition('id', 1)
|
||||
->condition('id', '2')
|
||||
->execute();
|
||||
$this->assertResults(['1', '2']);
|
||||
|
||||
// Simplify it with IN.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', ['1', '2'])
|
||||
->execute();
|
||||
$this->assertResults(['1', '2']);
|
||||
// Try explicit IN.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', ['1', '2'], 'IN')
|
||||
->execute();
|
||||
$this->assertResults(['1', '2']);
|
||||
// Try not IN.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', ['1', '2'], 'NOT IN')
|
||||
->execute();
|
||||
$this->assertResults(['3', '4', '5']);
|
||||
|
||||
// Filter with an OR condition group on different fields.
|
||||
$this->queryResults = $this->factory->get('config_query_test', 'OR')
|
||||
$this->queryResults = $this->entityStorage->getQuery('OR')
|
||||
->condition('id', 1)
|
||||
->condition('number', 41)
|
||||
->execute();
|
||||
@@ -253,14 +260,14 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
|
||||
// Filter with an OR condition group on different fields but matching on the
|
||||
// same entity.
|
||||
$this->queryResults = $this->factory->get('config_query_test', 'OR')
|
||||
$this->queryResults = $this->entityStorage->getQuery('OR')
|
||||
->condition('id', 1)
|
||||
->condition('number', 31)
|
||||
->execute();
|
||||
$this->assertResults(['1']);
|
||||
|
||||
// NO simple conditions, YES complex conditions, 'AND'.
|
||||
$query = $this->factory->get('config_query_test', 'AND');
|
||||
$query = $this->entityStorage->getQuery('AND');
|
||||
$and_condition_1 = $query->orConditionGroup()
|
||||
->condition('id', '2')
|
||||
->condition('label', $this->entities[0]->label);
|
||||
@@ -274,7 +281,7 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
$this->assertResults(['1']);
|
||||
|
||||
// NO simple conditions, YES complex conditions, 'OR'.
|
||||
$query = $this->factory->get('config_query_test', 'OR');
|
||||
$query = $this->entityStorage->getQuery('OR');
|
||||
$and_condition_1 = $query->andConditionGroup()
|
||||
->condition('id', 1)
|
||||
->condition('label', $this->entities[0]->label);
|
||||
@@ -288,7 +295,7 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
$this->assertResults(['1', '2']);
|
||||
|
||||
// YES simple conditions, YES complex conditions, 'AND'.
|
||||
$query = $this->factory->get('config_query_test', 'AND');
|
||||
$query = $this->entityStorage->getQuery('AND');
|
||||
$and_condition_1 = $query->orConditionGroup()
|
||||
->condition('id', '2')
|
||||
->condition('label', $this->entities[0]->label);
|
||||
@@ -303,7 +310,7 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
$this->assertResults(['1']);
|
||||
|
||||
// YES simple conditions, YES complex conditions, 'OR'.
|
||||
$query = $this->factory->get('config_query_test', 'OR');
|
||||
$query = $this->entityStorage->getQuery('OR');
|
||||
$and_condition_1 = $query->orConditionGroup()
|
||||
->condition('id', '2')
|
||||
->condition('label', $this->entities[0]->label);
|
||||
@@ -318,22 +325,22 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
$this->assertResults(['1', '2', '4', '5']);
|
||||
|
||||
// Test the exists and notExists conditions.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->exists('id')
|
||||
->execute();
|
||||
$this->assertResults(['1', '2', '3', '4', '5']);
|
||||
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->exists('non-existent')
|
||||
->execute();
|
||||
$this->assertResults([]);
|
||||
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->notExists('id')
|
||||
->execute();
|
||||
$this->assertResults([]);
|
||||
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->notExists('non-existent')
|
||||
->execute();
|
||||
$this->assertResults(['1', '2', '3', '4', '5']);
|
||||
@@ -353,43 +360,43 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
$entity->save();
|
||||
|
||||
// Test 'STARTS_WITH' condition.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', 'foo.bar', 'STARTS_WITH')
|
||||
->execute();
|
||||
$this->assertResults(['foo.bar']);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', 'f', 'STARTS_WITH')
|
||||
->execute();
|
||||
$this->assertResults(['foo.bar']);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', 'miss', 'STARTS_WITH')
|
||||
->execute();
|
||||
$this->assertResults([]);
|
||||
|
||||
// Test 'CONTAINS' condition.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', 'foo.bar', 'CONTAINS')
|
||||
->execute();
|
||||
$this->assertResults(['foo.bar']);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', 'oo.ba', 'CONTAINS')
|
||||
->execute();
|
||||
$this->assertResults(['foo.bar']);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', 'miss', 'CONTAINS')
|
||||
->execute();
|
||||
$this->assertResults([]);
|
||||
|
||||
// Test 'ENDS_WITH' condition.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', 'foo.bar', 'ENDS_WITH')
|
||||
->execute();
|
||||
$this->assertResults(['foo.bar']);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', 'r', 'ENDS_WITH')
|
||||
->execute();
|
||||
$this->assertResults(['foo.bar']);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', 'miss', 'ENDS_WITH')
|
||||
->execute();
|
||||
$this->assertResults([]);
|
||||
@@ -400,13 +407,13 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
*/
|
||||
public function testCount() {
|
||||
// Test count on no conditions.
|
||||
$count = $this->factory->get('config_query_test')
|
||||
$count = $this->entityStorage->getQuery()
|
||||
->count()
|
||||
->execute();
|
||||
$this->assertIdentical($count, count($this->entities));
|
||||
|
||||
// Test count on a complex query.
|
||||
$query = $this->factory->get('config_query_test', 'OR');
|
||||
$query = $this->entityStorage->getQuery('OR');
|
||||
$and_condition_1 = $query->andConditionGroup()
|
||||
->condition('id', 1)
|
||||
->condition('label', $this->entities[0]->label);
|
||||
@@ -426,51 +433,51 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
*/
|
||||
public function testSortRange() {
|
||||
// Sort by simple ascending/descending.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->sort('number', 'DESC')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['3', '5', '2', '1', '4']);
|
||||
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->sort('number', 'ASC')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['4', '1', '2', '5', '3']);
|
||||
|
||||
// Apply some filters and sort.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', '3', '>')
|
||||
->sort('number', 'DESC')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['5', '4']);
|
||||
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('id', '3', '>')
|
||||
->sort('number', 'ASC')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['4', '5']);
|
||||
|
||||
// Apply a pager and sort.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->sort('number', 'DESC')
|
||||
->range('2', '2')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['2', '1']);
|
||||
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->sort('number', 'ASC')
|
||||
->range('2', '2')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['2', '5']);
|
||||
|
||||
// Add a range to a query without a start parameter.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->range(0, '3')
|
||||
->sort('id', 'ASC')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['1', '2', '3']);
|
||||
|
||||
// Apply a pager with limit 4.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->pager('4', 0)
|
||||
->sort('id', 'ASC')
|
||||
->execute();
|
||||
@@ -488,28 +495,28 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
|
||||
// Sort key: id
|
||||
// Sorting with 'DESC' upper case
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->tableSort($header)
|
||||
->sort('id', 'DESC')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['5', '4', '3', '2', '1']);
|
||||
|
||||
// Sorting with 'ASC' upper case
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->tableSort($header)
|
||||
->sort('id', 'ASC')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['1', '2', '3', '4', '5']);
|
||||
|
||||
// Sorting with 'desc' lower case
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->tableSort($header)
|
||||
->sort('id', 'desc')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['5', '4', '3', '2', '1']);
|
||||
|
||||
// Sorting with 'asc' lower case
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->tableSort($header)
|
||||
->sort('id', 'asc')
|
||||
->execute();
|
||||
@@ -517,28 +524,28 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
|
||||
// Sort key: number
|
||||
// Sorting with 'DeSc' mixed upper and lower case
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->tableSort($header)
|
||||
->sort('number', 'DeSc')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['3', '5', '2', '1', '4']);
|
||||
|
||||
// Sorting with 'AsC' mixed upper and lower case
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->tableSort($header)
|
||||
->sort('number', 'AsC')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['4', '1', '2', '5', '3']);
|
||||
|
||||
// Sorting with 'dEsC' mixed upper and lower case
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->tableSort($header)
|
||||
->sort('number', 'dEsC')
|
||||
->execute();
|
||||
$this->assertIdentical(array_values($this->queryResults), ['3', '5', '2', '1', '4']);
|
||||
|
||||
// Sorting with 'aSc' mixed upper and lower case
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->tableSort($header)
|
||||
->sort('number', 'aSc')
|
||||
->execute();
|
||||
@@ -549,53 +556,53 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
* Tests dotted path matching.
|
||||
*/
|
||||
public function testDotted() {
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('array.level1.*', 1)
|
||||
->execute();
|
||||
$this->assertResults(['1', '3']);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('*.level1.level2', 2)
|
||||
->execute();
|
||||
$this->assertResults(['2', '4']);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('array.level1.*', 3)
|
||||
->execute();
|
||||
$this->assertResults(['5']);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('array.level1.level2', 3)
|
||||
->execute();
|
||||
$this->assertResults(['5']);
|
||||
// Make sure that values on the wildcard level do not match if there are
|
||||
// sub-keys defined. This must not find anything even if entity 2 has a
|
||||
// top-level key number with value 41.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('*.level1.level2', 41)
|
||||
->execute();
|
||||
$this->assertResults([]);
|
||||
// Make sure that "IS NULL" and "IS NOT NULL" work correctly with
|
||||
// array-valued fields/keys.
|
||||
$all = ['1', '2', '3', '4', '5'];
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->exists('array.level1.level2')
|
||||
->execute();
|
||||
$this->assertResults($all);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->exists('array.level1')
|
||||
->execute();
|
||||
$this->assertResults($all);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->exists('array')
|
||||
->execute();
|
||||
$this->assertResults($all);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->notExists('array.level1.level2')
|
||||
->execute();
|
||||
$this->assertResults([]);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->notExists('array.level1')
|
||||
->execute();
|
||||
$this->assertResults([]);
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->notExists('array')
|
||||
->execute();
|
||||
$this->assertResults([]);
|
||||
@@ -606,12 +613,12 @@ class ConfigEntityQueryTest extends KernelTestBase {
|
||||
*/
|
||||
public function testCaseSensitivity() {
|
||||
// Filter by label with a known containing case-sensitive word.
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('label', 'TEST', 'CONTAINS')
|
||||
->execute();
|
||||
$this->assertResults(['3', '4', '5']);
|
||||
|
||||
$this->queryResults = $this->factory->get('config_query_test')
|
||||
$this->queryResults = $this->entityStorage->getQuery()
|
||||
->condition('label', 'test', 'CONTAINS')
|
||||
->execute();
|
||||
$this->assertResults(['3', '4', '5']);
|
||||
|
||||
@@ -297,7 +297,7 @@ class ContentEntityCloneTest extends EntityKernelTestBase {
|
||||
// Retrieve the entity properties.
|
||||
$reflection = new \ReflectionClass($entity);
|
||||
$properties = $reflection->getProperties(~\ReflectionProperty::IS_STATIC);
|
||||
$translation_unique_properties = ['activeLangcode', 'translationInitialize', 'fieldDefinitions', 'languages', 'langcodeKey', 'defaultLangcode', 'defaultLangcodeKey', 'revisionTranslationAffectedKey', 'validated', 'validationRequired', 'entityTypeId', 'typedData', 'cacheContexts', 'cacheTags', 'cacheMaxAge', '_serviceIds'];
|
||||
$translation_unique_properties = ['activeLangcode', 'translationInitialize', 'fieldDefinitions', 'languages', 'langcodeKey', 'defaultLangcode', 'defaultLangcodeKey', 'revisionTranslationAffectedKey', 'validated', 'validationRequired', 'entityTypeId', 'typedData', 'cacheContexts', 'cacheTags', 'cacheMaxAge', '_serviceIds', '_entityStorages'];
|
||||
|
||||
foreach ($properties as $property) {
|
||||
// Modify each entity property on the clone and assert that the change is
|
||||
@@ -306,9 +306,17 @@ class ContentEntityCloneTest extends EntityKernelTestBase {
|
||||
$property->setValue($entity, 'default-value');
|
||||
$property->setValue($translation, 'default-value');
|
||||
$property->setValue($clone, 'test-entity-cloning');
|
||||
$this->assertEquals('default-value', $property->getValue($entity), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()]));
|
||||
$this->assertEquals('default-value', $property->getValue($translation), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()]));
|
||||
$this->assertEquals('test-entity-cloning', $property->getValue($clone), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()]));
|
||||
// Static properties remain the same across all instances of the class.
|
||||
if ($property->isStatic()) {
|
||||
$this->assertEquals('test-entity-cloning', $property->getValue($entity), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()]));
|
||||
$this->assertEquals('test-entity-cloning', $property->getValue($translation), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()]));
|
||||
$this->assertEquals('test-entity-cloning', $property->getValue($clone), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()]));
|
||||
}
|
||||
else {
|
||||
$this->assertEquals('default-value', $property->getValue($entity), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()]));
|
||||
$this->assertEquals('default-value', $property->getValue($translation), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()]));
|
||||
$this->assertEquals('test-entity-cloning', $property->getValue($clone), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()]));
|
||||
}
|
||||
|
||||
// Modify each entity property on the translation entity object and assert
|
||||
// that the change is propagated to the default translation entity object
|
||||
|
||||
@@ -43,6 +43,7 @@ class ContentEntityNullStorageTest extends KernelTestBase {
|
||||
* @see \Drupal\Core\Entity\Event\BundleConfigImportValidate
|
||||
*/
|
||||
public function testDeleteThroughImport() {
|
||||
$this->installConfig(['system']);
|
||||
$contact_form = ContactForm::create(['id' => 'test']);
|
||||
$contact_form->save();
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ class DefaultTableMappingIntegrationTest extends EntityKernelTestBase {
|
||||
/**
|
||||
* The table mapping for the tested entity type.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\Sql\TableMappingInterface
|
||||
* @var \Drupal\Core\Entity\Sql\DefaultTableMapping
|
||||
*/
|
||||
protected $tableMapping;
|
||||
|
||||
@@ -39,11 +39,18 @@ class DefaultTableMappingIntegrationTest extends EntityKernelTestBase {
|
||||
->setName('multivalued_base_field')
|
||||
->setTargetEntityTypeId('entity_test_mulrev')
|
||||
->setTargetBundle('entity_test_mulrev')
|
||||
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
|
||||
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
|
||||
// Base fields are non-translatable and non-revisionable by default, but
|
||||
// we explicitly set these values here for extra clarity.
|
||||
->setTranslatable(FALSE)
|
||||
->setRevisionable(FALSE);
|
||||
$this->state->set('entity_test_mulrev.additional_base_field_definitions', $definitions);
|
||||
|
||||
$this->entityManager->clearCachedDefinitions();
|
||||
$this->tableMapping = $this->entityManager->getStorage('entity_test_mulrev')->getTableMapping();
|
||||
|
||||
// Ensure that the tables for the new field are created.
|
||||
\Drupal::entityDefinitionUpdateManager()->applyUpdates();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,4 +75,33 @@ class DefaultTableMappingIntegrationTest extends EntityKernelTestBase {
|
||||
$this->assertEquals($this->tableMapping->getFieldTableName('multivalued_base_field'), $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests DefaultTableMapping::getTableNames().
|
||||
*
|
||||
* @covers ::getTableNames
|
||||
*/
|
||||
public function testGetTableNames() {
|
||||
$storage_definitions = $this->entityManager->getFieldStorageDefinitions('entity_test_mulrev');
|
||||
$dedicated_data_table = $this->tableMapping->getDedicatedDataTableName($storage_definitions['multivalued_base_field']);
|
||||
$dedicated_revision_table = $this->tableMapping->getDedicatedRevisionTableName($storage_definitions['multivalued_base_field']);
|
||||
|
||||
// Check that both the data and the revision tables exist for a multi-valued
|
||||
// base field.
|
||||
$database_schema = \Drupal::database()->schema();
|
||||
$this->assertTrue($database_schema->tableExists($dedicated_data_table));
|
||||
$this->assertTrue($database_schema->tableExists($dedicated_revision_table));
|
||||
|
||||
// Check that the table mapping contains both the data and the revision
|
||||
// tables exist for a multi-valued base field.
|
||||
$expected = [
|
||||
'entity_test_mulrev',
|
||||
'entity_test_mulrev_property_data',
|
||||
'entity_test_mulrev_revision',
|
||||
'entity_test_mulrev_property_revision',
|
||||
$dedicated_data_table,
|
||||
$dedicated_revision_table,
|
||||
];
|
||||
$this->assertEquals($expected, $this->tableMapping->getTableNames());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -65,7 +65,7 @@ class EntityAutocompleteElementFormTest extends EntityKernelTestBase implements
|
||||
|
||||
for ($i = 1; $i < 3; $i++) {
|
||||
$entity = EntityTest::create([
|
||||
'name' => $this->randomMachineName()
|
||||
'name' => $this->randomMachineName(),
|
||||
]);
|
||||
$entity->save();
|
||||
$this->referencedEntities[] = $entity;
|
||||
@@ -297,7 +297,7 @@ class EntityAutocompleteElementFormTest extends EntityKernelTestBase implements
|
||||
$form_state = (new FormState())
|
||||
->setValues([
|
||||
'single_no_validate' => 'single - non-existent label',
|
||||
'single_autocreate_no_validate' => 'single - autocreate non-existent label'
|
||||
'single_autocreate_no_validate' => 'single - autocreate non-existent label',
|
||||
]);
|
||||
$form_builder->submitForm($this, $form_state);
|
||||
|
||||
@@ -309,7 +309,7 @@ class EntityAutocompleteElementFormTest extends EntityKernelTestBase implements
|
||||
$form_state = (new FormState())
|
||||
->setValues([
|
||||
'single_no_validate' => 'single - non-existent label (42)',
|
||||
'single_autocreate_no_validate' => 'single - autocreate non-existent label (43)'
|
||||
'single_autocreate_no_validate' => 'single - autocreate non-existent label (43)',
|
||||
]);
|
||||
$form_builder->submitForm($this, $form_state);
|
||||
|
||||
|
||||
@@ -334,13 +334,13 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
|
||||
$this->assertFalse($entity->get('uuid')->access('edit'));
|
||||
$access_result = $entity->get('uuid')->access('edit', NULL, TRUE);
|
||||
$this->assertTrue($access_result->isForbidden());
|
||||
$this->assertEquals('The entity UUID cannot be changed', $access_result->getReason());
|
||||
$this->assertEquals('The entity UUID cannot be changed.', $access_result->getReason());
|
||||
|
||||
// Ensure the ID is still not allowed to be edited.
|
||||
$this->assertFalse($entity->get('id')->access('edit'));
|
||||
$access_result = $entity->get('id')->access('edit', NULL, TRUE);
|
||||
$this->assertTrue($access_result->isForbidden());
|
||||
$this->assertEquals('The entity ID cannot be changed', $access_result->getReason());
|
||||
$this->assertEquals('The entity ID cannot be changed.', $access_result->getReason());
|
||||
}
|
||||
|
||||
public function providerTestFieldAccess() {
|
||||
@@ -351,7 +351,7 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
|
||||
'name' => 'A test entity',
|
||||
'uuid' => '60e3a179-79ed-4653-ad52-5e614c8e8fbe',
|
||||
],
|
||||
FALSE
|
||||
FALSE,
|
||||
],
|
||||
'string ID entity' => [
|
||||
EntityTestStringId::class,
|
||||
@@ -360,7 +360,7 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
|
||||
'name' => 'A test entity',
|
||||
'uuid' => '60e3a179-79ed-4653-ad52-5e614c8e8fbe',
|
||||
],
|
||||
TRUE
|
||||
TRUE,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ class EntityCrudHookTest extends EntityKernelTestBase {
|
||||
/**
|
||||
* Checks the order of CRUD hook execution messages.
|
||||
*
|
||||
* entity_crud_hook_test.module implements all core entity CRUD hooks and
|
||||
* Module entity_crud_hook_test implements all core entity CRUD hooks and
|
||||
* stores a message for each in $GLOBALS['entity_crud_hook_test'].
|
||||
*
|
||||
* @param $messages
|
||||
|
||||
@@ -588,4 +588,58 @@ class EntityDecoupledTranslationRevisionsTest extends EntityKernelTestBase {
|
||||
$this->assertFalse($en_revision->hasTranslation('it'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the revision create hook works as expected.
|
||||
*
|
||||
* @covers ::createRevision
|
||||
*/
|
||||
public function testCreateRevisionHook() {
|
||||
$entity = EntityTestMulRev::create();
|
||||
$entity->get('name')->value = 'revision_create_test_en';
|
||||
$this->storage->save($entity);
|
||||
|
||||
/** @var \Drupal\Core\Entity\ContentEntityInterface $translation */
|
||||
$translation = $entity->addTranslation('it');
|
||||
$translation->set('name', 'revision_create_test_it');
|
||||
/** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
|
||||
$revision = $this->storage->createRevision($translation, FALSE, TRUE);
|
||||
|
||||
// Assert that the alter hook can alter the new revision.
|
||||
$this->assertEquals('revision_create_test_it_altered', $revision->get('name')->value);
|
||||
|
||||
// Assert the data passed to the hook.
|
||||
$data = $this->state->get('entity_test.hooks');
|
||||
$this->assertEquals('revision_create_test_it', $data['entity_test_mulrev_revision_create']['entity']->get('name')->value);
|
||||
$this->assertEquals('revision_create_test_it_altered', $data['entity_test_mulrev_revision_create']['new_revision']->get('name')->value);
|
||||
$this->assertFalse($data['entity_test_mulrev_revision_create']['entity']->isNewRevision());
|
||||
$this->assertTrue($data['entity_test_mulrev_revision_create']['new_revision']->isNewRevision());
|
||||
$this->assertTrue($data['entity_test_mulrev_revision_create']['entity']->isDefaultRevision());
|
||||
$this->assertFalse($data['entity_test_mulrev_revision_create']['new_revision']->isDefaultRevision());
|
||||
$this->assertTrue($data['entity_test_mulrev_revision_create']['keep_untranslatable_fields']);
|
||||
|
||||
$this->assertEquals('revision_create_test_it', $data['entity_revision_create']['entity']->get('name')->value);
|
||||
$this->assertEquals('revision_create_test_it_altered', $data['entity_revision_create']['new_revision']->get('name')->value);
|
||||
$this->assertFalse($data['entity_revision_create']['entity']->isNewRevision());
|
||||
$this->assertTrue($data['entity_revision_create']['new_revision']->isNewRevision());
|
||||
$this->assertTrue($data['entity_revision_create']['entity']->isDefaultRevision());
|
||||
$this->assertFalse($data['entity_revision_create']['new_revision']->isDefaultRevision());
|
||||
$this->assertTrue($data['entity_revision_create']['keep_untranslatable_fields']);
|
||||
|
||||
// Test again with different arguments.
|
||||
$translation->isDefaultRevision(FALSE);
|
||||
$this->storage->createRevision($translation);
|
||||
$data = $this->state->get('entity_test.hooks');
|
||||
$this->assertFalse($data['entity_revision_create']['entity']->isNewRevision());
|
||||
$this->assertTrue($data['entity_revision_create']['new_revision']->isNewRevision());
|
||||
$this->assertFalse($data['entity_revision_create']['entity']->isDefaultRevision());
|
||||
$this->assertTrue($data['entity_revision_create']['new_revision']->isDefaultRevision());
|
||||
$this->assertNull($data['entity_revision_create']['keep_untranslatable_fields']);
|
||||
|
||||
$this->assertFalse($data['entity_test_mulrev_revision_create']['entity']->isNewRevision());
|
||||
$this->assertTrue($data['entity_test_mulrev_revision_create']['new_revision']->isNewRevision());
|
||||
$this->assertFalse($data['entity_test_mulrev_revision_create']['entity']->isDefaultRevision());
|
||||
$this->assertTrue($data['entity_test_mulrev_revision_create']['new_revision']->isDefaultRevision());
|
||||
$this->assertNull($data['entity_test_mulrev_revision_create']['keep_untranslatable_fields']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,11 +15,13 @@ use Drupal\Core\Field\FieldException;
|
||||
use Drupal\Core\Field\FieldStorageDefinitionEvents;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\entity_test_update\Entity\EntityTestUpdate;
|
||||
use Drupal\system\Tests\Entity\EntityDefinitionTestTrait;
|
||||
use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
|
||||
|
||||
/**
|
||||
* Tests EntityDefinitionUpdateManager functionality.
|
||||
*
|
||||
* @coversDefaultClass \Drupal\Core\Entity\EntityDefinitionUpdateManager
|
||||
*
|
||||
* @group Entity
|
||||
*/
|
||||
class EntityDefinitionUpdateTest extends EntityKernelTestBase {
|
||||
@@ -1049,31 +1051,78 @@ class EntityDefinitionUpdateTest extends EntityKernelTestBase {
|
||||
|
||||
/**
|
||||
* Tests adding a base field with initial values inherited from another field.
|
||||
*
|
||||
* @dataProvider initialValueFromFieldTestCases
|
||||
*/
|
||||
public function testInitialValueFromField() {
|
||||
public function testInitialValueFromField($default_initial_value, $expected_value) {
|
||||
$storage = \Drupal::entityTypeManager()->getStorage('entity_test_update');
|
||||
$db_schema = $this->database->schema();
|
||||
|
||||
// Create two entities before adding the base field.
|
||||
/** @var \Drupal\entity_test\Entity\EntityTestUpdate $entity */
|
||||
$storage->create(['name' => 'First entity'])->save();
|
||||
$storage->create(['name' => 'Second entity'])->save();
|
||||
/** @var \Drupal\entity_test_update\Entity\EntityTestUpdate $entity */
|
||||
$storage->create([
|
||||
'name' => 'First entity',
|
||||
'test_single_property' => 'test existing value',
|
||||
])->save();
|
||||
|
||||
// The second entity does not have any value for the 'test_single_property'
|
||||
// field, allowing us to test the 'default_value' parameter of
|
||||
// \Drupal\Core\Field\BaseFieldDefinition::setInitialValueFromField().
|
||||
$storage->create([
|
||||
'name' => 'Second entity',
|
||||
])->save();
|
||||
|
||||
// Add a base field with an initial value inherited from another field.
|
||||
$this->addBaseField();
|
||||
$storage_definition = BaseFieldDefinition::create('string')
|
||||
->setLabel(t('A new base field'))
|
||||
$definitions['new_base_field'] = BaseFieldDefinition::create('string')
|
||||
->setName('new_base_field')
|
||||
->setLabel('A new base field')
|
||||
->setInitialValueFromField('name');
|
||||
$definitions['another_base_field'] = BaseFieldDefinition::create('string')
|
||||
->setName('another_base_field')
|
||||
->setLabel('Another base field')
|
||||
->setInitialValueFromField('test_single_property', $default_initial_value);
|
||||
|
||||
$this->state->set('entity_test_update.additional_base_field_definitions', $definitions);
|
||||
|
||||
$this->assertFalse($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' does not exist before applying the update.");
|
||||
$this->entityDefinitionUpdateManager->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $storage_definition);
|
||||
$this->assertFalse($db_schema->fieldExists('entity_test_update', 'another_base_field'), "New field 'another_base_field' does not exist before applying the update.");
|
||||
$this->entityDefinitionUpdateManager->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $definitions['new_base_field']);
|
||||
$this->entityDefinitionUpdateManager->installFieldStorageDefinition('another_base_field', 'entity_test_update', 'entity_test', $definitions['another_base_field']);
|
||||
$this->assertTrue($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' has been created on the 'entity_test_update' table.");
|
||||
$this->assertTrue($db_schema->fieldExists('entity_test_update', 'another_base_field'), "New field 'another_base_field' has been created on the 'entity_test_update' table.");
|
||||
|
||||
// Check that the initial values have been applied.
|
||||
$storage = \Drupal::entityTypeManager()->getStorage('entity_test_update');
|
||||
$entities = $storage->loadMultiple();
|
||||
$this->assertEquals('First entity', $entities[1]->get('new_base_field')->value);
|
||||
$this->assertEquals('Second entity', $entities[2]->get('new_base_field')->value);
|
||||
|
||||
$this->assertEquals('test existing value', $entities[1]->get('another_base_field')->value);
|
||||
$this->assertEquals($expected_value, $entities[2]->get('another_base_field')->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test cases for ::testInitialValueFromField.
|
||||
*/
|
||||
public function initialValueFromFieldTestCases() {
|
||||
return [
|
||||
'literal value' => [
|
||||
'test initial value',
|
||||
'test initial value',
|
||||
],
|
||||
'indexed array' => [
|
||||
['value' => 'test initial value'],
|
||||
'test initial value',
|
||||
],
|
||||
'empty array' => [
|
||||
[],
|
||||
NULL,
|
||||
],
|
||||
'null' => [
|
||||
NULL,
|
||||
NULL,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1133,4 +1182,18 @@ class EntityDefinitionUpdateTest extends EntityKernelTestBase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getEntityTypes
|
||||
*/
|
||||
public function testGetEntityTypes() {
|
||||
$entity_type_definitions = $this->entityDefinitionUpdateManager->getEntityTypes();
|
||||
|
||||
// Ensure that we have at least one entity type to check below.
|
||||
$this->assertGreaterThanOrEqual(1, count($entity_type_definitions));
|
||||
|
||||
foreach ($entity_type_definitions as $entity_type_id => $entity_type) {
|
||||
$this->assertEquals($this->entityDefinitionUpdateManager->getEntityType($entity_type_id), $entity_type);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\Component\Uuid\Uuid;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
|
||||
/**
|
||||
* Tests default values for entity fields.
|
||||
@@ -47,8 +47,8 @@ class EntityFieldDefaultValueTest extends EntityKernelTestBase {
|
||||
->create();
|
||||
$definition = $this->entityManager->getDefinition($entity_type_id);
|
||||
$langcode_key = $definition->getKey('langcode');
|
||||
$this->assertEqual($entity->{$langcode_key}->value, 'en', SafeMarkup::format('%entity_type: Default language', ['%entity_type' => $entity_type_id]));
|
||||
$this->assertTrue(Uuid::isValid($entity->uuid->value), SafeMarkup::format('%entity_type: Default UUID', ['%entity_type' => $entity_type_id]));
|
||||
$this->assertEqual($entity->{$langcode_key}->value, 'en', new FormattableMarkup('%entity_type: Default language', ['%entity_type' => $entity_type_id]));
|
||||
$this->assertTrue(Uuid::isValid($entity->uuid->value), new FormattableMarkup('%entity_type: Default UUID', ['%entity_type' => $entity_type_id]));
|
||||
$this->assertEqual($entity->name->getValue(), [], 'Field has one empty value by default.');
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ use Drupal\Core\TypedData\DataDefinitionInterface;
|
||||
use Drupal\Core\TypedData\ListInterface;
|
||||
use Drupal\Core\TypedData\Type\StringInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInterface;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\entity_test\Entity\EntityTestComputedField;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
@@ -870,6 +871,32 @@ class EntityFieldTest extends EntityKernelTestBase {
|
||||
$this->assertFalse($computed_item_list1->equals($computed_item_list2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests an entity reference computed field.
|
||||
*/
|
||||
public function testEntityReferenceComputedField() {
|
||||
$this->installEntitySchema('entity_test_computed_field');
|
||||
|
||||
// Create 2 entities to be referenced.
|
||||
$ref1 = EntityTest::create(['name' => 'foo', 'type' => 'bar']);
|
||||
$ref1->save();
|
||||
$ref2 = EntityTest::create(['name' => 'baz', 'type' => 'bar']);
|
||||
$ref2->save();
|
||||
\Drupal::state()->set('entity_test_reference_computed_target_ids', [$ref1->id(), $ref2->id()]);
|
||||
|
||||
$entity = EntityTestComputedField::create([]);
|
||||
$entity->save();
|
||||
|
||||
/** @var \Drupal\entity_test\Plugin\Field\ComputedReferenceTestFieldItemList $field */
|
||||
$field = $entity->get('computed_reference_field');
|
||||
/** @var \Drupal\Core\Entity\EntityInterface[] $referenced_entities */
|
||||
$referenced_entities = $field->referencedEntities();
|
||||
|
||||
// Check that ::referencedEntities() is working with computed fields.
|
||||
$this->assertEquals($ref1->id(), $referenced_entities[0]->id());
|
||||
$this->assertEquals($ref2->id(), $referenced_entities[1]->id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the computed properties tests for the given entity type.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
||||
/**
|
||||
* @covers \Drupal\Core\Entity\Plugin\Validation\Constraint\EntityHasFieldConstraintValidator
|
||||
*
|
||||
* @group Entity
|
||||
*/
|
||||
class EntityHasFieldConstraintValidatorTest extends EntityKernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['entity_test_constraints'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('entity_test_constraints');
|
||||
$this->createUser();
|
||||
}
|
||||
|
||||
public function testValidation() {
|
||||
$this->state->set('entity_test_constraints.build', [
|
||||
'EntityHasField' => 'body',
|
||||
]);
|
||||
|
||||
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
|
||||
$entity_type_manager = $this->container->get('entity_type.manager');
|
||||
$entity_type_manager->clearCachedDefinitions();
|
||||
|
||||
// Clear the typed data cache so that the entity has the correct constraints
|
||||
// during validation.
|
||||
$this->container->get('typed_data_manager')->clearCachedDefinitions();
|
||||
|
||||
$storage = $entity_type_manager->getStorage('entity_test_constraints');
|
||||
|
||||
/** @var \Drupal\entity_test\Entity\EntityTestConstraints $entity */
|
||||
$entity = $storage->create();
|
||||
// We should get a violation if we try to validate the entity before the
|
||||
// field has been created.
|
||||
$violations = $entity->validate();
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertEquals($violations[0]->getMessage(), 'The entity must have the <em class="placeholder">body</em> field.');
|
||||
$storage->save($entity);
|
||||
|
||||
// Create the field.
|
||||
$field_storage = FieldStorageConfig::create([
|
||||
'type' => 'string',
|
||||
'entity_type' => $entity->getEntityTypeId(),
|
||||
'field_name' => 'body',
|
||||
]);
|
||||
$field_storage->save();
|
||||
|
||||
FieldConfig::create([
|
||||
'field_storage' => $field_storage,
|
||||
'bundle' => $entity->bundle(),
|
||||
])->save();
|
||||
|
||||
// Now that the field has been created, there should be no violations.
|
||||
$this->assertCount(0, $storage->loadUnchanged(1)->validate());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
|
||||
/**
|
||||
* Test the behavior of entity keys.
|
||||
*
|
||||
* @group entity
|
||||
*/
|
||||
class EntityKeysTest extends EntityKernelTestBase {
|
||||
|
||||
/**
|
||||
* Test the cache when multiple keys reference a single field.
|
||||
*
|
||||
* @dataProvider multipleKeysCacheTestCases
|
||||
*/
|
||||
public function testMultipleKeysCache($translatable) {
|
||||
$this->state->set('entity_test.additional_base_field_definitions', [
|
||||
'test_field' => BaseFieldDefinition::create('string')->setTranslatable($translatable),
|
||||
]);
|
||||
$this->state->set('entity_test.entity_keys', [
|
||||
'key_1' => 'test_field',
|
||||
'key_2' => 'test_field',
|
||||
]);
|
||||
drupal_flush_all_caches();
|
||||
$this->installEntitySchema('entity_test');
|
||||
|
||||
$entity = EntityTest::create([]);
|
||||
|
||||
$entity->set('test_field', 'foo');
|
||||
$this->assertEquals('foo', $entity->getEntityKey('key_1'));
|
||||
$this->assertEquals('foo', $entity->getEntityKey('key_2'));
|
||||
|
||||
$entity->set('test_field', 'bar');
|
||||
$this->assertEquals('bar', $entity->getEntityKey('key_1'));
|
||||
$this->assertEquals('bar', $entity->getEntityKey('key_2'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for ::testMultipleKeysCache.
|
||||
*/
|
||||
public function multipleKeysCacheTestCases() {
|
||||
return [
|
||||
'translatable Entity Key' => [
|
||||
TRUE,
|
||||
],
|
||||
'Non-translatable entity key' => [
|
||||
FALSE,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
@@ -64,10 +63,10 @@ abstract class EntityLanguageTestBase extends EntityKernelTestBase {
|
||||
$this->state->set('entity_test.translation', TRUE);
|
||||
|
||||
// Create a translatable test field.
|
||||
$this->fieldName = Unicode::strtolower($this->randomMachineName() . '_field_name');
|
||||
$this->fieldName = mb_strtolower($this->randomMachineName() . '_field_name');
|
||||
|
||||
// Create an untranslatable test field.
|
||||
$this->untranslatableFieldName = Unicode::strtolower($this->randomMachineName() . '_field_name');
|
||||
$this->untranslatableFieldName = mb_strtolower($this->randomMachineName() . '_field_name');
|
||||
|
||||
// Create field fields in all entity variations.
|
||||
foreach (entity_test_entity_types() as $entity_type) {
|
||||
|
||||
@@ -35,12 +35,12 @@ class EntityLoadByUuidTest extends KernelTestBase {
|
||||
// Create two test entities.
|
||||
$entity_0 = EntityTest::create([
|
||||
'type' => 'entity_test',
|
||||
'name' => 'published entity'
|
||||
'name' => 'published entity',
|
||||
]);
|
||||
$entity_0->save();
|
||||
$entity_1 = EntityTest::create([
|
||||
'type' => 'entity_test',
|
||||
'name' => 'unpublished entity'
|
||||
'name' => 'unpublished entity',
|
||||
]);
|
||||
$entity_1->save();
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
/**
|
||||
* The entity_test storage to create the test entities.
|
||||
*
|
||||
* @var \Drupal\entity_test\EntityTestStorage
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $entityStorage;
|
||||
|
||||
@@ -34,18 +34,10 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
*/
|
||||
protected $queryResult;
|
||||
|
||||
/**
|
||||
* The query factory to create entity queries.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\Query\QueryFactory
|
||||
*/
|
||||
public $factory;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->entityStorage = $this->entityManager->getStorage('entity_test');
|
||||
$this->factory = $this->container->get('entity.query');
|
||||
|
||||
// Add some fieldapi fields to be used in the test.
|
||||
for ($i = 1; $i <= 2; $i++) {
|
||||
@@ -120,7 +112,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
*/
|
||||
public function testAggregation() {
|
||||
// Apply a simple groupby.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('user_id')
|
||||
->execute();
|
||||
|
||||
@@ -139,14 +131,14 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Apply a simple aggregation for different aggregation functions.
|
||||
foreach ($function_expected as $aggregation_function => $expected) {
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', $aggregation_function)
|
||||
->execute();
|
||||
$this->assertEqual($this->queryResult, $expected);
|
||||
}
|
||||
|
||||
// Apply aggregation and groupby on the same query.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', 'COUNT')
|
||||
->groupBy('user_id')
|
||||
->execute();
|
||||
@@ -157,7 +149,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
]);
|
||||
|
||||
// Apply aggregation and a condition which matches.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', 'COUNT')
|
||||
->groupBy('id')
|
||||
->conditionAggregate('id', 'COUNT', 8)
|
||||
@@ -165,14 +157,14 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
$this->assertResults([]);
|
||||
|
||||
// Don't call aggregate to test the implicit aggregate call.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('id')
|
||||
->conditionAggregate('id', 'COUNT', 8)
|
||||
->execute();
|
||||
$this->assertResults([]);
|
||||
|
||||
// Apply aggregation and a condition which matches.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', 'count')
|
||||
->groupBy('id')
|
||||
->conditionAggregate('id', 'COUNT', 6)
|
||||
@@ -181,7 +173,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Apply aggregation, a groupby and a condition which matches partially via
|
||||
// the operator '='.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', 'count')
|
||||
->conditionAggregate('id', 'count', 2)
|
||||
->groupBy('user_id')
|
||||
@@ -190,7 +182,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Apply aggregation, a groupby and a condition which matches partially via
|
||||
// the operator '>'.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', 'count')
|
||||
->conditionAggregate('id', 'COUNT', 1, '>')
|
||||
->groupBy('user_id')
|
||||
@@ -202,20 +194,20 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Apply aggregation and a sort. This might not be useful, but have a proper
|
||||
// test coverage.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', 'COUNT')
|
||||
->sortAggregate('id', 'COUNT')
|
||||
->execute();
|
||||
$this->assertSortedResults([['id_count' => 6]]);
|
||||
|
||||
// Don't call aggregate to test the implicit aggregate call.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->sortAggregate('id', 'COUNT')
|
||||
->execute();
|
||||
$this->assertSortedResults([['id_count' => 6]]);
|
||||
|
||||
// Apply aggregation, groupby and a sort descending.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', 'COUNT')
|
||||
->groupBy('user_id')
|
||||
->sortAggregate('id', 'COUNT', 'DESC')
|
||||
@@ -227,7 +219,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
]);
|
||||
|
||||
// Apply aggregation, groupby and a sort ascending.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', 'COUNT')
|
||||
->groupBy('user_id')
|
||||
->sortAggregate('id', 'COUNT', 'ASC')
|
||||
@@ -240,7 +232,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Apply aggregation, groupby, an aggregation condition and a sort with the
|
||||
// operator '='.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', 'COUNT')
|
||||
->groupBy('user_id')
|
||||
->sortAggregate('id', 'COUNT')
|
||||
@@ -250,7 +242,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Apply aggregation, groupby, an aggregation condition and a sort with the
|
||||
// operator '<' and order ASC.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', 'COUNT')
|
||||
->groupBy('user_id')
|
||||
->sortAggregate('id', 'COUNT', 'ASC')
|
||||
@@ -263,7 +255,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Apply aggregation, groupby, an aggregation condition and a sort with the
|
||||
// operator '<' and order DESC.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('id', 'COUNT')
|
||||
->groupBy('user_id')
|
||||
->sortAggregate('id', 'COUNT', 'DESC')
|
||||
@@ -277,7 +269,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
// Test aggregation/groupby support for fieldapi fields.
|
||||
|
||||
// Just group by a fieldapi field.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('field_test_1')
|
||||
->execute();
|
||||
$this->assertResults([
|
||||
@@ -287,7 +279,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
]);
|
||||
|
||||
// Group by a fieldapi field and aggregate a normal property.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('user_id', 'COUNT')
|
||||
->groupBy('field_test_1')
|
||||
->execute();
|
||||
@@ -299,7 +291,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
]);
|
||||
|
||||
// Group by a normal property and aggregate a fieldapi field.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('field_test_1', 'COUNT')
|
||||
->groupBy('user_id')
|
||||
->execute();
|
||||
@@ -310,7 +302,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
['user_id' => 3, 'field_test_1_count' => 2],
|
||||
]);
|
||||
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('field_test_1', 'SUM')
|
||||
->groupBy('user_id')
|
||||
->execute();
|
||||
@@ -321,7 +313,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
]);
|
||||
|
||||
// Aggregate by two different fieldapi fields.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('field_test_1', 'SUM')
|
||||
->aggregate('field_test_2', 'SUM')
|
||||
->groupBy('user_id')
|
||||
@@ -333,7 +325,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
]);
|
||||
|
||||
// This time aggregate the same field twice.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('field_test_1', 'SUM')
|
||||
->aggregate('field_test_1', 'COUNT')
|
||||
->groupBy('user_id')
|
||||
@@ -345,7 +337,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
]);
|
||||
|
||||
// Group by and aggregate by a fieldapi field.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('field_test_1')
|
||||
->aggregate('field_test_2', 'COUNT')
|
||||
->execute();
|
||||
@@ -357,7 +349,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Group by and aggregate by a fieldapi field and use multiple aggregate
|
||||
// functions.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('field_test_1')
|
||||
->aggregate('field_test_2', 'COUNT')
|
||||
->aggregate('field_test_2', 'SUM')
|
||||
@@ -370,7 +362,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Apply an aggregate condition for a fieldapi field and group by a simple
|
||||
// property.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->conditionAggregate('field_test_1', 'COUNT', 3)
|
||||
->groupBy('user_id')
|
||||
->execute();
|
||||
@@ -379,7 +371,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
['user_id' => 3, 'field_test_1_count' => 2],
|
||||
]);
|
||||
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('field_test_1', 'SUM')
|
||||
->conditionAggregate('field_test_1', 'COUNT', 2, '>')
|
||||
->groupBy('user_id')
|
||||
@@ -391,7 +383,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Apply an aggregate condition for a simple property and a group by a
|
||||
// fieldapi field.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->conditionAggregate('user_id', 'COUNT', 2)
|
||||
->groupBy('field_test_1')
|
||||
->execute();
|
||||
@@ -399,7 +391,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
['field_test_1' => 1, 'user_id_count' => 2],
|
||||
]);
|
||||
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->conditionAggregate('user_id', 'COUNT', 2, '>')
|
||||
->groupBy('field_test_1')
|
||||
->execute();
|
||||
@@ -409,14 +401,14 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
]);
|
||||
|
||||
// Apply an aggregate condition and a group by fieldapi fields.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('field_test_1')
|
||||
->conditionAggregate('field_test_2', 'COUNT', 2)
|
||||
->execute();
|
||||
$this->assertResults([
|
||||
['field_test_1' => 1, 'field_test_2_count' => 2],
|
||||
]);
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('field_test_1')
|
||||
->conditionAggregate('field_test_2', 'COUNT', 2, '>')
|
||||
->execute();
|
||||
@@ -427,7 +419,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Apply an aggregate condition and a group by fieldapi fields with multiple
|
||||
// conditions via AND.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('field_test_1')
|
||||
->conditionAggregate('field_test_2', 'COUNT', 2)
|
||||
->conditionAggregate('field_test_2', 'SUM', 8)
|
||||
@@ -436,7 +428,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Apply an aggregate condition and a group by fieldapi fields with multiple
|
||||
// conditions via OR.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test', 'OR')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery('OR')
|
||||
->groupBy('field_test_1')
|
||||
->conditionAggregate('field_test_2', 'COUNT', 2)
|
||||
->conditionAggregate('field_test_2', 'SUM', 8)
|
||||
@@ -448,7 +440,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Group by a normal property and aggregate a fieldapi field and sort by the
|
||||
// groupby field.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('field_test_1', 'COUNT')
|
||||
->groupBy('user_id')
|
||||
->sort('user_id', 'DESC')
|
||||
@@ -459,7 +451,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
['user_id' => 1, 'field_test_1_count' => 1],
|
||||
]);
|
||||
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->aggregate('field_test_1', 'COUNT')
|
||||
->groupBy('user_id')
|
||||
->sort('user_id', 'ASC')
|
||||
@@ -470,7 +462,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
['user_id' => 3, 'field_test_1_count' => 2],
|
||||
]);
|
||||
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->conditionAggregate('field_test_1', 'COUNT', 2, '>')
|
||||
->groupBy('user_id')
|
||||
->sort('user_id', 'ASC')
|
||||
@@ -482,7 +474,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Group by a normal property, aggregate a fieldapi field, and sort by the
|
||||
// aggregated field.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->sortAggregate('field_test_1', 'COUNT', 'DESC')
|
||||
->groupBy('user_id')
|
||||
->execute();
|
||||
@@ -492,7 +484,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
['user_id' => 1, 'field_test_1_count' => 1],
|
||||
]);
|
||||
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->sortAggregate('field_test_1', 'COUNT', 'ASC')
|
||||
->groupBy('user_id')
|
||||
->execute();
|
||||
@@ -503,7 +495,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
]);
|
||||
|
||||
// Group by and aggregate by fieldapi field, and sort by the groupby field.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('field_test_1')
|
||||
->aggregate('field_test_2', 'COUNT')
|
||||
->sort('field_test_1', 'ASC')
|
||||
@@ -514,7 +506,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
['field_test_1' => 3, 'field_test_2_count' => 1],
|
||||
]);
|
||||
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('field_test_1')
|
||||
->aggregate('field_test_2', 'COUNT')
|
||||
->sort('field_test_1', 'DESC')
|
||||
@@ -527,7 +519,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
|
||||
// Groupby and aggregate by fieldapi field, and sort by the aggregated
|
||||
// field.
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('field_test_1')
|
||||
->sortAggregate('field_test_2', 'COUNT', 'DESC')
|
||||
->execute();
|
||||
@@ -537,7 +529,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
|
||||
['field_test_1' => 3, 'field_test_2_count' => 1],
|
||||
]);
|
||||
|
||||
$this->queryResult = $this->factory->getAggregate('entity_test')
|
||||
$this->queryResult = $this->entityStorage->getAggregateQuery()
|
||||
->groupBy('field_test_1')
|
||||
->sortAggregate('field_test_2', 'COUNT', 'ASC')
|
||||
->execute();
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
|
||||
use Drupal\taxonomy\Entity\Vocabulary;
|
||||
@@ -25,11 +24,6 @@ class EntityQueryRelationshipTest extends EntityKernelTestBase {
|
||||
*/
|
||||
public static $modules = ['taxonomy'];
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Entity\Query\QueryFactory
|
||||
*/
|
||||
protected $factory;
|
||||
|
||||
/**
|
||||
* Term entities.
|
||||
*
|
||||
@@ -73,7 +67,7 @@ class EntityQueryRelationshipTest extends EntityKernelTestBase {
|
||||
// We want an entity reference field. It needs a vocabulary, terms, a field
|
||||
// storage and a field. First, create the vocabulary.
|
||||
$vocabulary = Vocabulary::create([
|
||||
'vid' => Unicode::strtolower($this->randomMachineName()),
|
||||
'vid' => mb_strtolower($this->randomMachineName()),
|
||||
]);
|
||||
$vocabulary->save();
|
||||
|
||||
@@ -110,88 +104,88 @@ class EntityQueryRelationshipTest extends EntityKernelTestBase {
|
||||
$entity->save();
|
||||
$this->entities[] = $entity;
|
||||
}
|
||||
$this->factory = \Drupal::service('entity.query');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests querying.
|
||||
*/
|
||||
public function testQuery() {
|
||||
$storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
|
||||
// This returns the 0th entity as that's the only one pointing to the 0th
|
||||
// account.
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->condition("user_id.entity.name", $this->accounts[0]->getUsername())
|
||||
->execute();
|
||||
$this->assertResults([0]);
|
||||
// This returns the 1st and 2nd entity as those point to the 1st account.
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->condition("user_id.entity.name", $this->accounts[0]->getUsername(), '<>')
|
||||
->execute();
|
||||
$this->assertResults([1, 2]);
|
||||
// This returns all three entities because all of them point to an
|
||||
// account.
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->exists("user_id.entity.name")
|
||||
->execute();
|
||||
$this->assertResults([0, 1, 2]);
|
||||
// This returns no entities because all of them point to an account.
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->notExists("user_id.entity.name")
|
||||
->execute();
|
||||
$this->assertEqual(count($this->queryResults), 0);
|
||||
// This returns the 0th entity as that's only one pointing to the 0th
|
||||
// term (test without specifying the field column).
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->condition("$this->fieldName.entity.name", $this->terms[0]->name->value)
|
||||
->execute();
|
||||
$this->assertResults([0]);
|
||||
// This returns the 0th entity as that's only one pointing to the 0th
|
||||
// term (test with specifying the column name).
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->condition("$this->fieldName.target_id.entity.name", $this->terms[0]->name->value)
|
||||
->execute();
|
||||
$this->assertResults([0]);
|
||||
// This returns the 1st and 2nd entity as those point to the 1st term.
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->condition("$this->fieldName.entity.name", $this->terms[0]->name->value, '<>')
|
||||
->execute();
|
||||
$this->assertResults([1, 2]);
|
||||
// This returns the 0th entity as that's only one pointing to the 0th
|
||||
// account.
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->condition("user_id.entity:user.name", $this->accounts[0]->getUsername())
|
||||
->execute();
|
||||
$this->assertResults([0]);
|
||||
// This returns the 1st and 2nd entity as those point to the 1st account.
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->condition("user_id.entity:user.name", $this->accounts[0]->getUsername(), '<>')
|
||||
->execute();
|
||||
$this->assertResults([1, 2]);
|
||||
// This returns all three entities because all of them point to an
|
||||
// account.
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->exists("user_id.entity:user.name")
|
||||
->execute();
|
||||
$this->assertResults([0, 1, 2]);
|
||||
// This returns no entities because all of them point to an account.
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->notExists("user_id.entity:user.name")
|
||||
->execute();
|
||||
$this->assertEqual(count($this->queryResults), 0);
|
||||
// This returns the 0th entity as that's only one pointing to the 0th
|
||||
// term (test without specifying the field column).
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value)
|
||||
->execute();
|
||||
$this->assertResults([0]);
|
||||
// This returns the 0th entity as that's only one pointing to the 0th
|
||||
// term (test with specifying the column name).
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->condition("$this->fieldName.target_id.entity:taxonomy_term.name", $this->terms[0]->name->value)
|
||||
->execute();
|
||||
$this->assertResults([0]);
|
||||
// This returns the 1st and 2nd entity as those point to the 1st term.
|
||||
$this->queryResults = $this->factory->get('entity_test')
|
||||
$this->queryResults = $storage->getQuery()
|
||||
->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value, '<>')
|
||||
->execute();
|
||||
$this->assertResults([1, 2]);
|
||||
@@ -202,8 +196,10 @@ class EntityQueryRelationshipTest extends EntityKernelTestBase {
|
||||
*/
|
||||
public function testInvalidSpecifier() {
|
||||
$this->setExpectedException(PluginNotFoundException::class);
|
||||
$this->factory
|
||||
->get('taxonomy_term')
|
||||
$this->container
|
||||
->get('entity_type.manager')
|
||||
->getStorage('taxonomy_term')
|
||||
->getQuery()
|
||||
->condition('langcode.language.foo', 'bar')
|
||||
->execute();
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\entity_test\Entity\EntityTestMulRev;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
@@ -34,11 +33,6 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
*/
|
||||
protected $queryResults;
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Entity\Query\QueryFactory
|
||||
*/
|
||||
protected $factory;
|
||||
|
||||
/**
|
||||
* A list of bundle machine names created for this test.
|
||||
*
|
||||
@@ -60,6 +54,13 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
*/
|
||||
public $figures;
|
||||
|
||||
/**
|
||||
* The entity_test_mulrev entity storage.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
@@ -67,8 +68,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
|
||||
$this->installConfig(['language']);
|
||||
|
||||
$figures = Unicode::strtolower($this->randomMachineName());
|
||||
$greetings = Unicode::strtolower($this->randomMachineName());
|
||||
$figures = mb_strtolower($this->randomMachineName());
|
||||
$greetings = mb_strtolower($this->randomMachineName());
|
||||
foreach ([$figures => 'shape', $greetings => 'text'] as $field_name => $field_type) {
|
||||
$field_storage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
@@ -146,7 +147,7 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->bundles = $bundles;
|
||||
$this->figures = $figures;
|
||||
$this->greetings = $greetings;
|
||||
$this->factory = \Drupal::service('entity.query');
|
||||
$this->storage = $this->container->get('entity_type.manager')->getStorage('entity_test_mulrev');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -155,7 +156,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
public function testEntityQuery() {
|
||||
$greetings = $this->greetings;
|
||||
$figures = $this->figures;
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->exists($greetings, 'tr')
|
||||
->condition("$figures.color", 'red')
|
||||
->sort('id')
|
||||
@@ -164,7 +166,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
// bit 0 and bit 2 needs to be set.
|
||||
$this->assertResult(5, 7, 13, 15);
|
||||
|
||||
$query = $this->factory->get('entity_test_mulrev', 'OR')
|
||||
$query = $this->storage
|
||||
->getQuery('OR')
|
||||
->exists($greetings, 'tr')
|
||||
->condition("$figures.color", 'red')
|
||||
->sort('id');
|
||||
@@ -176,7 +179,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertResult(1, 3, 4, 5, 6, 7, 9, 11, 12, 13, 14, 15);
|
||||
|
||||
// Test cloning of query conditions.
|
||||
$query = $this->factory->get('entity_test_mulrev')
|
||||
$query = $this->storage
|
||||
->getQuery()
|
||||
->condition("$figures.color", 'red')
|
||||
->sort('id');
|
||||
$cloned_query = clone $query;
|
||||
@@ -189,7 +193,7 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->queryResults = $cloned_query->execute();
|
||||
$this->assertResult();
|
||||
|
||||
$query = $this->factory->get('entity_test_mulrev');
|
||||
$query = $this->storage->getQuery();
|
||||
$group = $query->orConditionGroup()
|
||||
->exists($greetings, 'tr')
|
||||
->condition("$figures.color", 'red');
|
||||
@@ -202,7 +206,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertResult(9, 11, 12, 13, 14, 15);
|
||||
|
||||
// No figure has both the colors blue and red at the same time.
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("$figures.color", 'blue')
|
||||
->condition("$figures.color", 'red')
|
||||
->sort('id')
|
||||
@@ -210,7 +215,7 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertResult();
|
||||
|
||||
// But an entity might have a red and a blue figure both.
|
||||
$query = $this->factory->get('entity_test_mulrev');
|
||||
$query = $this->storage->getQuery();
|
||||
$group_blue = $query->andConditionGroup()->condition("$figures.color", 'blue');
|
||||
$group_red = $query->andConditionGroup()->condition("$figures.color", 'red');
|
||||
$this->queryResults = $query
|
||||
@@ -222,7 +227,7 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertResult(3, 7, 11, 15);
|
||||
|
||||
// Do the same test but with IN operator.
|
||||
$query = $this->factory->get('entity_test_mulrev');
|
||||
$query = $this->storage->getQuery();
|
||||
$group_blue = $query->andConditionGroup()->condition("$figures.color", ['blue'], 'IN');
|
||||
$group_red = $query->andConditionGroup()->condition("$figures.color", ['red'], 'IN');
|
||||
$this->queryResults = $query
|
||||
@@ -234,14 +239,16 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertResult(3, 7, 11, 15);
|
||||
|
||||
// An entity might have either red or blue figure.
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("$figures.color", ['blue', 'red'], 'IN')
|
||||
->sort('id')
|
||||
->execute();
|
||||
// Bit 0 or 1 is on.
|
||||
$this->assertResult(1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15);
|
||||
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->exists("$figures.color")
|
||||
->notExists("$greetings.value")
|
||||
->sort('id')
|
||||
@@ -250,7 +257,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertResult(1, 2, 3);
|
||||
// Now update the 'merhaba' string to xsiemax which is not a meaningful
|
||||
// word but allows us to test revisions and string operations.
|
||||
$ids = $this->factory->get('entity_test_mulrev')
|
||||
$ids = $this->storage
|
||||
->getQuery()
|
||||
->condition("$greetings.value", 'merhaba')
|
||||
->sort('id')
|
||||
->execute();
|
||||
@@ -264,30 +272,35 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$entity->save();
|
||||
}
|
||||
// We changed the entity names, so the current revision should not match.
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition('name.value', $old_name)
|
||||
->execute();
|
||||
$this->assertResult();
|
||||
// Only if all revisions are queried, we find the old revision.
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition('name.value', $old_name)
|
||||
->allRevisions()
|
||||
->sort('revision_id')
|
||||
->execute();
|
||||
$this->assertRevisionResult([$first_entity->id()], [$first_entity->id()]);
|
||||
// When querying current revisions, this string is no longer found.
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("$greetings.value", 'merhaba')
|
||||
->execute();
|
||||
$this->assertResult();
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("$greetings.value", 'merhaba')
|
||||
->allRevisions()
|
||||
->sort('revision_id')
|
||||
->execute();
|
||||
// The query only matches the original revisions.
|
||||
$this->assertRevisionResult([4, 5, 6, 7, 12, 13, 14, 15], [4, 5, 6, 7, 12, 13, 14, 15]);
|
||||
$results = $this->factory->get('entity_test_mulrev')
|
||||
$results = $this->storage
|
||||
->getQuery()
|
||||
->condition("$greetings.value", 'siema', 'CONTAINS')
|
||||
->sort('id')
|
||||
->execute();
|
||||
@@ -295,21 +308,24 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
// revisions are returned for some entities.
|
||||
$assert = [16 => '4', 17 => '5', 18 => '6', 19 => '7', 8 => '8', 9 => '9', 10 => '10', 11 => '11', 20 => '12', 21 => '13', 22 => '14', 23 => '15'];
|
||||
$this->assertIdentical($results, $assert);
|
||||
$results = $this->factory->get('entity_test_mulrev')
|
||||
$results = $this->storage
|
||||
->getQuery()
|
||||
->condition("$greetings.value", 'siema', 'STARTS_WITH')
|
||||
->sort('revision_id')
|
||||
->execute();
|
||||
// Now we only get the ones that originally were siema, entity id 8 and
|
||||
// above.
|
||||
$this->assertIdentical($results, array_slice($assert, 4, 8, TRUE));
|
||||
$results = $this->factory->get('entity_test_mulrev')
|
||||
$results = $this->storage
|
||||
->getQuery()
|
||||
->condition("$greetings.value", 'a', 'ENDS_WITH')
|
||||
->sort('revision_id')
|
||||
->execute();
|
||||
// It is very important that we do not get the ones which only have
|
||||
// xsiemax despite originally they were merhaba, ie. ended with a.
|
||||
$this->assertIdentical($results, array_slice($assert, 4, 8, TRUE));
|
||||
$results = $this->factory->get('entity_test_mulrev')
|
||||
$results = $this->storage
|
||||
->getQuery()
|
||||
->condition("$greetings.value", 'a', 'ENDS_WITH')
|
||||
->allRevisions()
|
||||
->sort('id')
|
||||
@@ -321,7 +337,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
|
||||
// Check that a query on the latest revisions without any condition returns
|
||||
// the correct results.
|
||||
$results = $this->factory->get('entity_test_mulrev')
|
||||
$results = $this->storage
|
||||
->getQuery()
|
||||
->latestRevision()
|
||||
->sort('id')
|
||||
->sort('revision_id')
|
||||
@@ -339,15 +356,18 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$greetings = $this->greetings;
|
||||
$figures = $this->figures;
|
||||
// Order up and down on a number.
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->sort('id')
|
||||
->execute();
|
||||
$this->assertResult(range(1, 15));
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->sort('id', 'DESC')
|
||||
->execute();
|
||||
$this->assertResult(range(15, 1));
|
||||
$query = $this->factory->get('entity_test_mulrev')
|
||||
$query = $this->storage
|
||||
->getQuery()
|
||||
->sort("$figures.color")
|
||||
->sort("$greetings.format")
|
||||
->sort('id');
|
||||
@@ -397,7 +417,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
'page' => '0,2',
|
||||
]);
|
||||
\Drupal::getContainer()->get('request_stack')->push($request);
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->sort("$figures.color")
|
||||
->sort("$greetings.format")
|
||||
->sort('id')
|
||||
@@ -406,7 +427,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertResult(15, 6, 7, 1);
|
||||
|
||||
// Now test the reversed order.
|
||||
$query = $this->factory->get('entity_test_mulrev')
|
||||
$query = $this->storage
|
||||
->getQuery()
|
||||
->sort("$figures.color", 'DESC')
|
||||
->sort("$greetings.format", 'DESC')
|
||||
->sort('id', 'DESC');
|
||||
@@ -435,7 +457,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
'type' => ['data' => 'Type', 'specifier' => 'type'],
|
||||
];
|
||||
|
||||
$this->queryResults = array_values($this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = array_values($this->storage
|
||||
->getQuery()
|
||||
->tableSort($header)
|
||||
->execute());
|
||||
$this->assertBundleOrder('asc');
|
||||
@@ -449,7 +472,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
'id' => ['data' => 'Id', 'specifier' => 'id'],
|
||||
'type' => ['data' => 'Type', 'specifier' => 'type'],
|
||||
];
|
||||
$this->queryResults = array_values($this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = array_values($this->storage
|
||||
->getQuery()
|
||||
->tableSort($header)
|
||||
->execute());
|
||||
$this->assertBundleOrder('desc');
|
||||
@@ -459,7 +483,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
'order' => 'Id',
|
||||
]);
|
||||
\Drupal::getContainer()->get('request_stack')->push($request);
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->tableSort($header)
|
||||
->execute();
|
||||
$this->assertResult(range(15, 1));
|
||||
@@ -494,7 +519,9 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
|
||||
// As the single entity of this type we just saved does not have a value
|
||||
// in the color field, the result should be 0.
|
||||
$count = $this->factory->get('entity_test')
|
||||
$count = $this->container->get('entity_type.manager')
|
||||
->getStorage('entity_test')
|
||||
->getQuery()
|
||||
->exists("$field_name.color")
|
||||
->count()
|
||||
->execute();
|
||||
@@ -507,7 +534,7 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
public function testNestedConditionGroups() {
|
||||
// Query for all entities of the first bundle that have either a red
|
||||
// triangle as a figure or the Turkish greeting as a greeting.
|
||||
$query = $this->factory->get('entity_test_mulrev');
|
||||
$query = $this->storage->getQuery();
|
||||
|
||||
$first_and = $query->andConditionGroup()
|
||||
->condition($this->figures . '.color', 'red')
|
||||
@@ -526,7 +553,32 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
->sort('id')
|
||||
->execute();
|
||||
|
||||
$this->assertResult(6, 14);
|
||||
$this->assertResult(4, 6, 12, 14);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that condition count returns expected number of conditions.
|
||||
*/
|
||||
public function testConditionCount() {
|
||||
// Query for all entities of the first bundle that
|
||||
// have red as a colour AND are triangle shaped.
|
||||
$query = $this->storage->getQuery();
|
||||
|
||||
// Add an AND condition group with 2 conditions in it.
|
||||
$and_condition_group = $query->andConditionGroup()
|
||||
->condition($this->figures . '.color', 'red')
|
||||
->condition($this->figures . '.shape', 'triangle');
|
||||
|
||||
// We added 2 conditions so count should be 2.
|
||||
$this->assertEqual($and_condition_group->count(), 2);
|
||||
|
||||
// Add an OR condition group with 2 conditions in it.
|
||||
$or_condition_group = $query->orConditionGroup()
|
||||
->condition($this->figures . '.color', 'red')
|
||||
->condition($this->figures . '.shape', 'triangle');
|
||||
|
||||
// We added 2 conditions so count should be 2.
|
||||
$this->assertEqual($or_condition_group->count(), 2);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -535,14 +587,16 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
public function testDelta() {
|
||||
$figures = $this->figures;
|
||||
// Test numeric delta value in field condition.
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("$figures.0.color", 'red')
|
||||
->sort('id')
|
||||
->execute();
|
||||
// As unit 0 at delta 0 was the red triangle bit 0 needs to be set.
|
||||
$this->assertResult(1, 3, 5, 7, 9, 11, 13, 15);
|
||||
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("$figures.1.color", 'red')
|
||||
->sort('id')
|
||||
->execute();
|
||||
@@ -550,7 +604,7 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertResult();
|
||||
|
||||
// Test on two different deltas.
|
||||
$query = $this->factory->get('entity_test_mulrev');
|
||||
$query = $this->storage->getQuery();
|
||||
$or = $query->andConditionGroup()
|
||||
->condition("$figures.0.color", 'red')
|
||||
->condition("$figures.1.color", 'blue');
|
||||
@@ -561,7 +615,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertResult(3, 7, 11, 15);
|
||||
|
||||
// Test the delta range condition.
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("$figures.%delta.color", ['blue', 'red'], 'IN')
|
||||
->condition("$figures.%delta", [0, 1], 'IN')
|
||||
->sort('id')
|
||||
@@ -570,7 +625,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertResult(1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15);
|
||||
|
||||
// Test the delta range condition without conditions on the value.
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("$figures.%delta", 1)
|
||||
->sort('id')
|
||||
->execute();
|
||||
@@ -579,12 +635,14 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
|
||||
// Numeric delta on single value base field should return results only if
|
||||
// the first item is being targeted.
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("id.0.value", [1, 3, 5], 'IN')
|
||||
->sort('id')
|
||||
->execute();
|
||||
$this->assertResult(1, 3, 5);
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("id.1.value", [1, 3, 5], 'IN')
|
||||
->sort('id')
|
||||
->execute();
|
||||
@@ -592,18 +650,21 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
|
||||
// Delta range condition on single value base field should return results
|
||||
// only if just the field value is targeted.
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("id.%delta.value", [1, 3, 5], 'IN')
|
||||
->sort('id')
|
||||
->execute();
|
||||
$this->assertResult(1, 3, 5);
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("id.%delta.value", [1, 3, 5], 'IN')
|
||||
->condition("id.%delta", 0, '=')
|
||||
->sort('id')
|
||||
->execute();
|
||||
$this->assertResult(1, 3, 5);
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition("id.%delta.value", [1, 3, 5], 'IN')
|
||||
->condition("id.%delta", 1, '=')
|
||||
->sort('id')
|
||||
@@ -660,7 +721,7 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
* The tags and metadata should propagate to the SQL query object.
|
||||
*/
|
||||
public function testMetaData() {
|
||||
$query = \Drupal::entityQuery('entity_test_mulrev');
|
||||
$query = $this->storage->getQuery();
|
||||
$query
|
||||
->addTag('efq_metadata_test')
|
||||
->addMetaData('foo', 'bar')
|
||||
@@ -684,7 +745,7 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
'translatable' => FALSE,
|
||||
'settings' => [
|
||||
'case_sensitive' => FALSE,
|
||||
]
|
||||
],
|
||||
]);
|
||||
$field_storage->save();
|
||||
|
||||
@@ -720,8 +781,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$string = $this->randomMachineName(7) . 'a';
|
||||
$fixtures[] = [
|
||||
'original' => $string,
|
||||
'uppercase' => Unicode::strtoupper($string),
|
||||
'lowercase' => Unicode::strtolower($string),
|
||||
'uppercase' => mb_strtoupper($string),
|
||||
'lowercase' => mb_strtolower($string),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -730,138 +791,161 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
'name' => $this->randomMachineName(),
|
||||
'langcode' => 'en',
|
||||
'field_ci' => $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'],
|
||||
'field_cs' => $fixtures[0]['uppercase'] . $fixtures[1]['lowercase']
|
||||
'field_cs' => $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'],
|
||||
])->save();
|
||||
|
||||
// Check the case insensitive field, = operator.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase']
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'])
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case insensitive, lowercase');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase']
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'])
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case insensitive, uppercase');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase']
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'])
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case insensitive, mixed.');
|
||||
|
||||
// Check the case sensitive field, = operator.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase']
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'])
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 0, 'Case sensitive, lowercase.');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase']
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'])
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 0, 'Case sensitive, uppercase.');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase']
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'])
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case sensitive, exact match.');
|
||||
|
||||
// Check the case insensitive field, IN operator.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', [$fixtures[0]['lowercase'] . $fixtures[1]['lowercase']], 'IN'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', [$fixtures[0]['lowercase'] . $fixtures[1]['lowercase']], 'IN')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case insensitive, lowercase');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', [$fixtures[0]['uppercase'] . $fixtures[1]['uppercase']], 'IN'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', [$fixtures[0]['uppercase'] . $fixtures[1]['uppercase']], 'IN')->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case insensitive, uppercase');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', [$fixtures[0]['uppercase'] . $fixtures[1]['lowercase']], 'IN'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', [$fixtures[0]['uppercase'] . $fixtures[1]['lowercase']], 'IN')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case insensitive, mixed');
|
||||
|
||||
// Check the case sensitive field, IN operator.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', [$fixtures[0]['lowercase'] . $fixtures[1]['lowercase']], 'IN'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', [$fixtures[0]['lowercase'] . $fixtures[1]['lowercase']], 'IN')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 0, 'Case sensitive, lowercase');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', [$fixtures[0]['uppercase'] . $fixtures[1]['uppercase']], 'IN'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', [$fixtures[0]['uppercase'] . $fixtures[1]['uppercase']], 'IN')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 0, 'Case sensitive, uppercase');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', [$fixtures[0]['uppercase'] . $fixtures[1]['lowercase']], 'IN'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', [$fixtures[0]['uppercase'] . $fixtures[1]['lowercase']], 'IN')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case sensitive, mixed');
|
||||
|
||||
// Check the case insensitive field, STARTS_WITH operator.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', $fixtures[0]['lowercase'], 'STARTS_WITH'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', $fixtures[0]['lowercase'], 'STARTS_WITH')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', $fixtures[0]['uppercase'], 'STARTS_WITH'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', $fixtures[0]['uppercase'], 'STARTS_WITH')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case sensitive, exact match.');
|
||||
|
||||
// Check the case sensitive field, STARTS_WITH operator.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', $fixtures[0]['lowercase'], 'STARTS_WITH'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', $fixtures[0]['lowercase'], 'STARTS_WITH')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 0, 'Case sensitive, lowercase.');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', $fixtures[0]['uppercase'], 'STARTS_WITH'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', $fixtures[0]['uppercase'], 'STARTS_WITH')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case sensitive, exact match.');
|
||||
|
||||
// Check the case insensitive field, ENDS_WITH operator.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', $fixtures[1]['lowercase'], 'ENDS_WITH'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', $fixtures[1]['lowercase'], 'ENDS_WITH')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', $fixtures[1]['uppercase'], 'ENDS_WITH'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', $fixtures[1]['uppercase'], 'ENDS_WITH')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case sensitive, exact match.');
|
||||
|
||||
// Check the case sensitive field, ENDS_WITH operator.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', $fixtures[1]['lowercase'], 'ENDS_WITH'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', $fixtures[1]['lowercase'], 'ENDS_WITH')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', $fixtures[1]['uppercase'], 'ENDS_WITH'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', $fixtures[1]['uppercase'], 'ENDS_WITH')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 0, 'Case sensitive, exact match.');
|
||||
|
||||
// Check the case insensitive field, CONTAINS operator, use the inner 8
|
||||
// characters of the uppercase and lowercase strings.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_ci', Unicode::strtolower(Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_ci', mb_strtolower(mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case sensitive, exact match.');
|
||||
|
||||
// Check the case sensitive field, CONTAINS operator.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.');
|
||||
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')->condition(
|
||||
'field_cs', Unicode::strtolower(Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS'
|
||||
)->execute();
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('field_cs', mb_strtolower(mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS')
|
||||
->execute();
|
||||
$this->assertIdentical(count($result), 0, 'Case sensitive, exact match.');
|
||||
|
||||
}
|
||||
@@ -895,7 +979,9 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
]);
|
||||
$term2->save();
|
||||
|
||||
$ids = \Drupal::entityQuery('taxonomy_term')
|
||||
$ids = $this->container->get('entity_type.manager')
|
||||
->getStorage('taxonomy_term')
|
||||
->getQuery()
|
||||
->condition('description.format', 'format1')
|
||||
->execute();
|
||||
|
||||
@@ -908,7 +994,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
*/
|
||||
public function testPendingRevisions() {
|
||||
// Ensure entity 14 is returned.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('id', [14], 'IN')
|
||||
->execute();
|
||||
$this->assertEqual(count($result), 1);
|
||||
@@ -921,24 +1008,27 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$entity->isDefaultRevision(FALSE);
|
||||
$entity->{$this->figures}->setValue([
|
||||
'color' => 'red',
|
||||
'shape' => 'square'
|
||||
'shape' => 'square',
|
||||
]);
|
||||
$entity->save();
|
||||
|
||||
// Entity query should still return entity 14.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('id', [14], 'IN')
|
||||
->execute();
|
||||
$this->assertEqual(count($result), 1);
|
||||
|
||||
// Verify that field conditions on the default and pending revision are
|
||||
// work as expected.
|
||||
$result = \Drupal::entityQuery('entity_test_mulrev')
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('id', [14], 'IN')
|
||||
->condition("$this->figures.color", $current_values[0]['color'])
|
||||
->execute();
|
||||
$this->assertEqual($result, [14 => '14']);
|
||||
$result = $this->factory->get('entity_test_mulrev')
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('id', [14], 'IN')
|
||||
->condition("$this->figures.color", 'red')
|
||||
->allRevisions()
|
||||
@@ -950,19 +1040,21 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$entity->isDefaultRevision(FALSE);
|
||||
$entity->{$this->figures}->setValue([
|
||||
'color' => 'red',
|
||||
'shape' => 'square'
|
||||
'shape' => 'square',
|
||||
]);
|
||||
$entity->save();
|
||||
|
||||
// A non-revisioned entity query should still return entity 14.
|
||||
$result = $this->factory->get('entity_test_mulrev')
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('id', [14], 'IN')
|
||||
->execute();
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertSame([14 => '14'], $result);
|
||||
|
||||
// Now check an entity query on the latest revision.
|
||||
$result = $this->factory->get('entity_test_mulrev')
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('id', [14], 'IN')
|
||||
->latestRevision()
|
||||
->execute();
|
||||
@@ -971,14 +1063,16 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
|
||||
// Verify that field conditions on the default and pending revision still
|
||||
// work as expected.
|
||||
$result = $this->factory->get('entity_test_mulrev')
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('id', [14], 'IN')
|
||||
->condition("$this->figures.color", $current_values[0]['color'])
|
||||
->execute();
|
||||
$this->assertSame([14 => '14'], $result);
|
||||
|
||||
// Now there are two revisions with same value for the figure color.
|
||||
$result = $this->factory->get('entity_test_mulrev')
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('id', [14], 'IN')
|
||||
->condition("$this->figures.color", 'red')
|
||||
->allRevisions()
|
||||
@@ -986,7 +1080,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertSame([16 => '14', 17 => '14'], $result);
|
||||
|
||||
// Check that querying for the latest revision returns the correct one.
|
||||
$result = $this->factory->get('entity_test_mulrev')
|
||||
$result = $this->storage
|
||||
->getQuery()
|
||||
->condition('id', [14], 'IN')
|
||||
->condition("$this->figures.color", 'red')
|
||||
->latestRevision()
|
||||
@@ -1000,7 +1095,8 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
*/
|
||||
public function testInjectionInCondition() {
|
||||
try {
|
||||
$this->queryResults = $this->factory->get('entity_test_mulrev')
|
||||
$this->queryResults = $this->storage
|
||||
->getQuery()
|
||||
->condition('1 ; -- ', [0, 1], 'IN')
|
||||
->sort('id')
|
||||
->execute();
|
||||
@@ -1019,6 +1115,9 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->createEntityReferenceField('entity_test', 'entity_test', 'ref1', $this->randomMachineName(), 'entity_test');
|
||||
$this->createEntityReferenceField('entity_test', 'entity_test', 'ref2', $this->randomMachineName(), 'entity_test');
|
||||
|
||||
$storage = $this->container->get('entity_type.manager')
|
||||
->getStorage('entity_test');
|
||||
|
||||
// Create two entities to be referred.
|
||||
$ref1 = EntityTest::create(['type' => 'entity_test']);
|
||||
$ref1->save();
|
||||
@@ -1034,7 +1133,7 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$entity->save();
|
||||
|
||||
// Check that works when referring with "{$field_name}".
|
||||
$result = $this->factory->get('entity_test')
|
||||
$result = $storage->getQuery()
|
||||
->condition('type', 'entity_test')
|
||||
->condition('ref1', $ref1->id())
|
||||
->condition('ref2', $ref2->id())
|
||||
@@ -1043,7 +1142,7 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertEquals($entity->id(), reset($result));
|
||||
|
||||
// Check that works when referring with "{$field_name}.target_id".
|
||||
$result = $this->factory->get('entity_test')
|
||||
$result = $storage->getQuery()
|
||||
->condition('type', 'entity_test')
|
||||
->condition('ref1.target_id', $ref1->id())
|
||||
->condition('ref2.target_id', $ref2->id())
|
||||
@@ -1052,7 +1151,7 @@ class EntityQueryTest extends EntityKernelTestBase {
|
||||
$this->assertEquals($entity->id(), reset($result));
|
||||
|
||||
// Check that works when referring with "{$field_name}.entity.id".
|
||||
$result = $this->factory->get('entity_test')
|
||||
$result = $storage->getQuery()
|
||||
->condition('type', 'entity_test')
|
||||
->condition('ref1.entity.id', $ref1->id())
|
||||
->condition('ref2.entity.id', $ref2->id())
|
||||
|
||||
@@ -212,7 +212,7 @@ class EntityReferenceFieldTest extends EntityKernelTestBase {
|
||||
// Create the default target entity.
|
||||
$target_entity = EntityTestStringId::create([
|
||||
'id' => $this->randomString(),
|
||||
'type' => $this->bundle
|
||||
'type' => $this->bundle,
|
||||
]);
|
||||
$target_entity->save();
|
||||
|
||||
@@ -381,7 +381,7 @@ class EntityReferenceFieldTest extends EntityKernelTestBase {
|
||||
$definitions = [
|
||||
'target_reference' => BaseFieldDefinition::create('entity_reference')
|
||||
->setSetting('target_type', $entity_type->id())
|
||||
->setSetting('handler', 'default')
|
||||
->setSetting('handler', 'default'),
|
||||
];
|
||||
$this->state->set('entity_test_update.additional_base_field_definitions', $definitions);
|
||||
$this->entityManager->clearCachedDefinitions();
|
||||
|
||||
@@ -161,7 +161,7 @@ class EntityRevisionTranslationTest extends EntityKernelTestBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Drupal\Core\Entity\RevisionableInterface::setNewRevision
|
||||
* @covers \Drupal\Core\Entity\ContentEntityBase::setNewRevision
|
||||
*/
|
||||
public function testSetNewRevision() {
|
||||
$user = $this->createUser();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
|
||||
/**
|
||||
* Tests adding a custom bundle field.
|
||||
@@ -86,27 +86,27 @@ class EntitySchemaTest extends EntityKernelTestBase {
|
||||
// Initially only the base table and the dedicated field data table should
|
||||
// exist.
|
||||
foreach ($tables as $index => $table) {
|
||||
$this->assertEqual($schema_handler->tableExists($table), !$index, SafeMarkup::format('Entity schema correct for the @table table.', ['@table' => $table]));
|
||||
$this->assertEqual($schema_handler->tableExists($table), !$index, new FormattableMarkup('Entity schema correct for the @table table.', ['@table' => $table]));
|
||||
}
|
||||
$this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), SafeMarkup::format('Field schema correct for the @table table.', ['@table' => $table]));
|
||||
$this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), new FormattableMarkup('Field schema correct for the @table table.', ['@table' => $table]));
|
||||
|
||||
// Update the entity type definition and check that the entity schema now
|
||||
// supports translations and revisions.
|
||||
$this->updateEntityType(TRUE);
|
||||
foreach ($tables as $table) {
|
||||
$this->assertTrue($schema_handler->tableExists($table), SafeMarkup::format('Entity schema correct for the @table table.', ['@table' => $table]));
|
||||
$this->assertTrue($schema_handler->tableExists($table), new FormattableMarkup('Entity schema correct for the @table table.', ['@table' => $table]));
|
||||
}
|
||||
foreach ($dedicated_tables as $table) {
|
||||
$this->assertTrue($schema_handler->tableExists($table), SafeMarkup::format('Field schema correct for the @table table.', ['@table' => $table]));
|
||||
$this->assertTrue($schema_handler->tableExists($table), new FormattableMarkup('Field schema correct for the @table table.', ['@table' => $table]));
|
||||
}
|
||||
|
||||
// Revert changes and check that the entity schema now does not support
|
||||
// neither translations nor revisions.
|
||||
$this->updateEntityType(FALSE);
|
||||
foreach ($tables as $index => $table) {
|
||||
$this->assertEqual($schema_handler->tableExists($table), !$index, SafeMarkup::format('Entity schema correct for the @table table.', ['@table' => $table]));
|
||||
$this->assertEqual($schema_handler->tableExists($table), !$index, new FormattableMarkup('Entity schema correct for the @table table.', ['@table' => $table]));
|
||||
}
|
||||
$this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), SafeMarkup::format('Field schema correct for the @table table.', ['@table' => $table]));
|
||||
$this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), new FormattableMarkup('Field schema correct for the @table table.', ['@table' => $table]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\Core\TypedData\TranslationStatusInterface;
|
||||
use Drupal\entity_test\Entity\EntityTestMul;
|
||||
use Drupal\entity_test\Entity\EntityTestMulRev;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
@@ -806,7 +807,7 @@ class EntityTranslationTest extends EntityLanguageTestBase {
|
||||
foreach ($langcodes as $langcode) {
|
||||
$adapter = $entity->getTranslation($langcode)->getTypedData();
|
||||
$name = $adapter->get('name')->value;
|
||||
$this->assertEqual($name, $values[$langcode]['name'], SafeMarkup::format('Name correctly retrieved from "@langcode" adapter', ['@langcode' => $langcode]));
|
||||
$this->assertEqual($name, $values[$langcode]['name'], new FormattableMarkup('Name correctly retrieved from "@langcode" adapter', ['@langcode' => $langcode]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1013,4 +1014,31 @@ class EntityTranslationTest extends EntityLanguageTestBase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the translation object cache.
|
||||
*/
|
||||
public function testTranslationObjectCache() {
|
||||
$default_langcode = $this->langcodes[1];
|
||||
$translation_langcode = $this->langcodes[2];
|
||||
|
||||
$entity = EntityTestMul::create([
|
||||
'name' => 'test',
|
||||
'langcode' => $default_langcode,
|
||||
]);
|
||||
$entity->save();
|
||||
$entity->addTranslation($translation_langcode)->save();
|
||||
|
||||
// Test that the default translation object is put into the translation
|
||||
// object cache when a new translation object is initialized.
|
||||
$entity = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
|
||||
$default_translation_spl_object_hash = spl_object_hash($entity);
|
||||
$this->assertEquals($default_translation_spl_object_hash, spl_object_hash($entity->getTranslation($translation_langcode)->getTranslation($default_langcode)));
|
||||
|
||||
// Test that non-default translations are always served from the translation
|
||||
// object cache.
|
||||
$entity = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
|
||||
$this->assertEquals(spl_object_hash($entity->getTranslation($translation_langcode)), spl_object_hash($entity->getTranslation($translation_langcode)));
|
||||
$this->assertEquals(spl_object_hash($entity->getTranslation($translation_langcode)), spl_object_hash($entity->getTranslation($translation_langcode)->getTranslation($default_langcode)->getTranslation($translation_langcode)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
|
||||
@@ -39,11 +40,6 @@ class EntityTypedDataDefinitionTest extends KernelTestBase {
|
||||
protected function setUp() {
|
||||
parent::setup();
|
||||
|
||||
NodeType::create([
|
||||
'type' => 'article',
|
||||
'name' => 'Article',
|
||||
])->save();
|
||||
|
||||
$this->typedDataManager = $this->container->get('typed_data_manager');
|
||||
}
|
||||
|
||||
@@ -90,6 +86,11 @@ class EntityTypedDataDefinitionTest extends KernelTestBase {
|
||||
* Tests deriving metadata about entities.
|
||||
*/
|
||||
public function testEntities() {
|
||||
NodeType::create([
|
||||
'type' => 'article',
|
||||
'name' => 'Article',
|
||||
])->save();
|
||||
|
||||
$entity_definition = EntityDataDefinition::create('node');
|
||||
$bundle_definition = EntityDataDefinition::create('node', 'article');
|
||||
// Entities are complex data.
|
||||
@@ -149,6 +150,7 @@ class EntityTypedDataDefinitionTest extends KernelTestBase {
|
||||
$entity_type_id = $this->randomMachineName();
|
||||
|
||||
$entity_type = $this->prophesize(EntityTypeInterface::class);
|
||||
$entity_type->entityClassImplements(ConfigEntityInterface::class)->willReturn(FALSE);
|
||||
$entity_type->getLabel()->willReturn($this->randomString());
|
||||
$entity_type->getConstraints()->willReturn([]);
|
||||
$entity_type->isInternal()->willReturn($internal);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Database\Database;
|
||||
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
@@ -89,7 +88,7 @@ class FieldSqlStorageTest extends EntityKernelTestBase {
|
||||
$this->fieldStorage->save();
|
||||
$this->field = FieldConfig::create([
|
||||
'field_storage' => $this->fieldStorage,
|
||||
'bundle' => $entity_type
|
||||
'bundle' => $entity_type,
|
||||
]);
|
||||
$this->field->save();
|
||||
|
||||
@@ -278,7 +277,7 @@ class FieldSqlStorageTest extends EntityKernelTestBase {
|
||||
$storage = $this->container->get('entity.manager')->getStorage($entity_type);
|
||||
|
||||
// Create two fields and generate random values.
|
||||
$name_base = Unicode::strtolower($this->randomMachineName(FieldStorageConfig::NAME_MAX_LENGTH - 1));
|
||||
$name_base = mb_strtolower($this->randomMachineName(FieldStorageConfig::NAME_MAX_LENGTH - 1));
|
||||
$field_names = [];
|
||||
$values = [];
|
||||
for ($i = 0; $i < 2; $i++) {
|
||||
@@ -380,8 +379,9 @@ class FieldSqlStorageTest extends EntityKernelTestBase {
|
||||
$this->tableMapping->getDedicatedDataTableName($prior_field_storage),
|
||||
$this->tableMapping->getDedicatedRevisionTableName($prior_field_storage),
|
||||
];
|
||||
$schema = Database::getConnection()->schema();
|
||||
foreach ($tables as $table_name) {
|
||||
$this->assertTrue(db_table_exists($table_name), t('Table %table exists.', ['%table' => $table_name]));
|
||||
$this->assertTrue($schema->tableExists($table_name), t('Table %table exists.', ['%table' => $table_name]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Form\FormState;
|
||||
use Drupal\entity_test\Entity\EntityTestCompositeConstraint;
|
||||
@@ -136,7 +136,7 @@ class FieldWidgetConstraintValidatorTest extends KernelTestBase {
|
||||
$errors = $this->getErrorsForEntity($entity, ['name']);
|
||||
$this->assertFalse(isset($errors['name']));
|
||||
$this->assertTrue(isset($errors['type']));
|
||||
$this->assertEqual($errors['type'], SafeMarkup::format('The validation failed because the value conflicts with the value in %field_name, which you cannot access.', ['%field_name' => 'name']));
|
||||
$this->assertEqual($errors['type'], new FormattableMarkup('The validation failed because the value conflicts with the value in %field_name, which you cannot access.', ['%field_name' => 'name']));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,7 +144,7 @@ class FieldWidgetConstraintValidatorTest extends KernelTestBase {
|
||||
*/
|
||||
public function testEntityLevelConstraintValidation() {
|
||||
$entity = EntityTestCompositeConstraint::create([
|
||||
'name' => 'entity-level-violation'
|
||||
'name' => 'entity-level-violation',
|
||||
]);
|
||||
$entity->save();
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ class RouteProviderTest extends KernelTestBase {
|
||||
|
||||
/** @var \Drupal\user\RoleInterface $role */
|
||||
$role = Role::create([
|
||||
'id' => RoleInterface::ANONYMOUS_ID
|
||||
'id' => RoleInterface::ANONYMOUS_ID,
|
||||
]);
|
||||
$role
|
||||
->grantPermission('administer entity_test content')
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity\Sql;
|
||||
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||
|
||||
/**
|
||||
* @group Entity
|
||||
*/
|
||||
class SqlContentEntityStorageSchemaTest extends EntityKernelTestBase {
|
||||
|
||||
/**
|
||||
* The key-value collection for tracking installed storage schema.
|
||||
*
|
||||
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
|
||||
*/
|
||||
protected $installedStorageSchema;
|
||||
|
||||
/**
|
||||
* The entity definition update manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
|
||||
*/
|
||||
protected $entityDefinitionUpdateManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
/* @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory */
|
||||
$key_value_factory = $this->container->get('keyvalue');
|
||||
$this->installedStorageSchema = $key_value_factory->get('entity.storage_schema.sql');
|
||||
$this->entityDefinitionUpdateManager = $this->container->get('entity.definition_update_manager');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating a shared table field definition.
|
||||
*/
|
||||
public function testOnFieldStorageDefinitionUpdateShared() {
|
||||
// Install the test entity type with an additional field. Use a multi-column
|
||||
// field so that field name and column name(s) do not match.
|
||||
$field = BaseFieldDefinition::create('shape')
|
||||
// Avoid creating a foreign key which is irrelevant for this test.
|
||||
->setSetting('foreign_key_name', NULL)
|
||||
->setName('shape')
|
||||
->setProvider('entity_test');
|
||||
$this->state->set('entity_test.additional_base_field_definitions', [
|
||||
'shape' => $field,
|
||||
]);
|
||||
$this->entityDefinitionUpdateManager->installFieldStorageDefinition(
|
||||
'shape',
|
||||
'entity_test',
|
||||
'entity_test',
|
||||
$field
|
||||
);
|
||||
|
||||
// Make sure the field is not marked as NOT NULL initially.
|
||||
$expected = [
|
||||
'entity_test' => [
|
||||
'fields' => [
|
||||
'shape__shape' => [
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => FALSE,
|
||||
],
|
||||
'shape__color' => [
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => FALSE,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$actual = $this->installedStorageSchema->get('entity_test.field_schema_data.shape');
|
||||
$this->assertSame($expected, $actual);
|
||||
|
||||
// Make the field an entity key, so that it will get marked as NOT NULL.
|
||||
$entity_type = $this->entityDefinitionUpdateManager->getEntityType('entity_test');
|
||||
$original_keys = $entity_type->getKeys();
|
||||
$entity_type->set('entity_keys', $original_keys + ['shape' => 'shape']);
|
||||
$this->entityDefinitionUpdateManager->updateEntityType($entity_type);
|
||||
|
||||
// Update the field and make sure the schema got updated.
|
||||
$this->entityDefinitionUpdateManager->updateFieldStorageDefinition($field);
|
||||
$expected['entity_test']['fields']['shape__shape']['not null'] = TRUE;
|
||||
$expected['entity_test']['fields']['shape__color']['not null'] = TRUE;
|
||||
$actual = $this->installedStorageSchema->get('entity_test.field_schema_data.shape');
|
||||
$this->assertSame($expected, $actual);
|
||||
|
||||
// Remove the entity key again and check that the schema got reverted.
|
||||
$entity_type->set('entity_keys', $original_keys);
|
||||
$this->entityDefinitionUpdateManager->updateEntityType($entity_type);
|
||||
|
||||
$this->entityDefinitionUpdateManager->updateFieldStorageDefinition($field);
|
||||
$expected['entity_test']['fields']['shape__shape']['not null'] = FALSE;
|
||||
$expected['entity_test']['fields']['shape__color']['not null'] = FALSE;
|
||||
$actual = $this->installedStorageSchema->get('entity_test.field_schema_data.shape');
|
||||
$this->assertSame($expected, $actual);
|
||||
|
||||
// Now add an entity and repeat the process.
|
||||
$entity_storage = $this->entityManager->getStorage('entity_test');
|
||||
$entity_storage->create([
|
||||
'shape' => [
|
||||
'shape' => 'rectangle',
|
||||
'color' => 'pink',
|
||||
],
|
||||
])->save();
|
||||
|
||||
$entity_type->set('entity_keys', $original_keys + ['shape' => 'shape']);
|
||||
$this->entityDefinitionUpdateManager->updateEntityType($entity_type);
|
||||
|
||||
$this->entityDefinitionUpdateManager->updateFieldStorageDefinition($field);
|
||||
$expected['entity_test']['fields']['shape__shape']['not null'] = TRUE;
|
||||
$expected['entity_test']['fields']['shape__color']['not null'] = TRUE;
|
||||
$actual = $this->installedStorageSchema->get('entity_test.field_schema_data.shape');
|
||||
$this->assertSame($expected, $actual);
|
||||
|
||||
$entity_type->set('entity_keys', $original_keys);
|
||||
$this->entityDefinitionUpdateManager->updateEntityType($entity_type);
|
||||
$this->entityDefinitionUpdateManager->updateFieldStorageDefinition($field);
|
||||
$expected['entity_test']['fields']['shape__shape']['not null'] = FALSE;
|
||||
$expected['entity_test']['fields']['shape__color']['not null'] = FALSE;
|
||||
$actual = $this->installedStorageSchema->get('entity_test.field_schema_data.shape');
|
||||
$this->assertSame($expected, $actual);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -150,7 +150,7 @@ class ValidReferenceConstraintValidatorTest extends EntityKernelTestBase {
|
||||
['entity' => $unpublished_node],
|
||||
['entity' => $different_bundle_node],
|
||||
['entity' => $deleted_node],
|
||||
]
|
||||
],
|
||||
]);
|
||||
|
||||
// Check that users with access are able pass the validation for fields
|
||||
|
||||
@@ -28,7 +28,7 @@ class IgnoreReplicaSubscriberTest extends KernelTestBase {
|
||||
Database::addConnectionInfo('default', 'replica', $connection_info['default']);
|
||||
|
||||
db_ignore_replica();
|
||||
$class_loader = require \Drupal::root() . '/autoload.php';
|
||||
$class_loader = require $this->root . '/autoload.php';
|
||||
$kernel = new DrupalKernel('testing', $class_loader, FALSE);
|
||||
$event = new GetResponseEvent($kernel, Request::create('http://example.com'), HttpKernelInterface::MASTER_REQUEST);
|
||||
$subscriber = new ReplicaDatabaseIgnoreSubscriber();
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Extension;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\Core\Extension\ModuleExtensionList
|
||||
* @group Extension
|
||||
*/
|
||||
class ModuleExtensionListTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* @covers ::getList
|
||||
*/
|
||||
public function testGetlist() {
|
||||
\Drupal::configFactory()->getEditable('core.extension')
|
||||
->set('module.testing', 1000)
|
||||
->set('profile', 'testing')
|
||||
->save();
|
||||
|
||||
// The installation profile is provided by a container parameter.
|
||||
// Saving the configuration doesn't automatically trigger invalidation
|
||||
$this->container->get('kernel')->rebuildContainer();
|
||||
|
||||
/** @var \Drupal\Core\Extension\ModuleExtensionList $module_extension_list */
|
||||
$module_extension_list = \Drupal::service('extension.list.module');
|
||||
$extensions = $module_extension_list->getList();
|
||||
|
||||
$this->assertArrayHasKey('testing', $extensions);
|
||||
$this->assertEquals(1000, $extensions['testing']->weight);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace Drupal\KernelTests\Core\Field\Entity;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\Core\Field\Entity\BaseFieldOverride;
|
||||
use Drupal\Core\Field\FieldItemList;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
@@ -18,7 +19,11 @@ class BaseFieldOverrideTest extends KernelTestBase {
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['system'];
|
||||
public static $modules = [
|
||||
'system',
|
||||
'user',
|
||||
'entity_test',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@@ -62,4 +67,29 @@ class BaseFieldOverrideTest extends KernelTestBase {
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the default value callback.
|
||||
*/
|
||||
public function testDefaultValueCallback() {
|
||||
$base_field = BaseFieldDefinition::create('entity_reference')
|
||||
->setName('Test Field')
|
||||
->setTargetEntityTypeId('entity_test')
|
||||
->setDefaultValueCallback(static::class . '::defaultValueCallbackPrimitive');
|
||||
$base_field_override = BaseFieldOverride::createFromBaseFieldDefinition($base_field, 'test_bundle');
|
||||
$entity = EntityTest::create([]);
|
||||
|
||||
$this->assertEquals([['target_id' => 99]], $base_field->getDefaultValue($entity));
|
||||
$this->assertEquals([['target_id' => 99]], $base_field_override->getDefaultValue($entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* A default value callback which returns a primitive value.
|
||||
*
|
||||
* @return int
|
||||
* A primitive default value.
|
||||
*/
|
||||
public static function defaultValueCallbackPrimitive() {
|
||||
return 99;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Field;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\entity_test\Entity\EntityTestMulRev;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
@@ -33,7 +32,7 @@ class FieldItemTest extends EntityKernelTestBase {
|
||||
$entity_type_id = 'entity_test_mulrev';
|
||||
$this->installEntitySchema($entity_type_id);
|
||||
|
||||
$this->fieldName = Unicode::strtolower($this->randomMachineName());
|
||||
$this->fieldName = mb_strtolower($this->randomMachineName());
|
||||
|
||||
/** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
|
||||
FieldStorageConfig::create([
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Field;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\entity_test\Entity\EntityTestMulRev;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
@@ -37,7 +36,7 @@ class FieldMissingTypeTest extends EntityKernelTestBase {
|
||||
|
||||
$entity_type_id = 'entity_test_mulrev';
|
||||
$this->installEntitySchema($entity_type_id);
|
||||
$this->fieldName = Unicode::strtolower($this->randomMachineName());
|
||||
$this->fieldName = mb_strtolower($this->randomMachineName());
|
||||
|
||||
/** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
|
||||
FieldStorageConfig::create([
|
||||
|
||||
@@ -82,7 +82,7 @@ class FieldSettingsTest extends EntityKernelTestBase {
|
||||
$field_storage = FieldStorageConfig::create([
|
||||
'field_name' => 'test_field',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'test_field'
|
||||
'type' => 'test_field',
|
||||
]);
|
||||
|
||||
// Check that the default settings have been populated.
|
||||
@@ -109,11 +109,11 @@ class FieldSettingsTest extends EntityKernelTestBase {
|
||||
$field_storage = FieldStorageConfig::create([
|
||||
'field_name' => 'test_field',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'test_field'
|
||||
'type' => 'test_field',
|
||||
]);
|
||||
$field = FieldConfig::create([
|
||||
'field_storage' => $field_storage,
|
||||
'bundle' => 'entity_test'
|
||||
'bundle' => 'entity_test',
|
||||
]);
|
||||
// Note: FieldConfig does not populate default settings until the config
|
||||
// is saved.
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\File;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Core\Site\Settings;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
@@ -82,7 +82,7 @@ class HtaccessTest extends KernelTestBase {
|
||||
*/
|
||||
protected function assertFilePermissions($uri, $expected) {
|
||||
$actual = fileperms($uri) & 0777;
|
||||
return $this->assertIdentical($actual, $expected, SafeMarkup::format('@uri file permissions @actual are identical to @expected.', [
|
||||
return $this->assertIdentical($actual, $expected, new FormattableMarkup('@uri file permissions @actual are identical to @expected.', [
|
||||
'@uri' => $uri,
|
||||
'@actual' => 0 . decoct($actual),
|
||||
'@expected' => 0 . decoct($expected),
|
||||
|
||||
@@ -62,7 +62,7 @@ class MimeTypeTest extends FileTestBase {
|
||||
'extensions' => [
|
||||
'jar' => 0,
|
||||
'jpg' => 1,
|
||||
]
|
||||
],
|
||||
];
|
||||
|
||||
$test_case = [
|
||||
|
||||
@@ -38,7 +38,8 @@ class NameMungingTest extends FileTestBase {
|
||||
// Disable insecure uploads.
|
||||
$this->config('system.file')->set('allow_insecure_uploads', 0)->save();
|
||||
$munged_name = file_munge_filename($this->name, '', TRUE);
|
||||
$messages = drupal_get_messages();
|
||||
$messages = \Drupal::messenger()->all();
|
||||
\Drupal::messenger()->deleteAll();
|
||||
$this->assertTrue(in_array(strtr('For security reasons, your upload has been renamed to <em class="placeholder">%filename</em>.', ['%filename' => $munged_name]), $messages['status']), 'Alert properly set when a file is renamed.');
|
||||
$this->assertNotEqual($munged_name, $this->name, format_string('The new filename (%munged) has been modified from the original (%original)', ['%munged' => $munged_name, '%original' => $this->name]));
|
||||
}
|
||||
|
||||
@@ -129,7 +129,6 @@ class ScanDirectoryTest extends FileTestBase {
|
||||
$this->assertEqual(2, count($files), 'With recursion we found the expected javascript files.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check that the min_depth options lets us ignore files in the starting
|
||||
* directory.
|
||||
|
||||
@@ -11,6 +11,7 @@ use Drupal\Core\File\FileSystem;
|
||||
* @group File
|
||||
*/
|
||||
class UnmanagedCopyTest extends FileTestBase {
|
||||
|
||||
/**
|
||||
* Copy a normal file.
|
||||
*/
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Drupal\KernelTests\Core\File;
|
||||
* @group File
|
||||
*/
|
||||
class UnmanagedDeleteRecursiveTest extends FileTestBase {
|
||||
|
||||
/**
|
||||
* Delete a normal file.
|
||||
*/
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Drupal\KernelTests\Core\File;
|
||||
* @group File
|
||||
*/
|
||||
class UnmanagedDeleteTest extends FileTestBase {
|
||||
|
||||
/**
|
||||
* Delete a normal file.
|
||||
*/
|
||||
|
||||
@@ -11,6 +11,7 @@ use Drupal\Core\File\FileSystem;
|
||||
* @group File
|
||||
*/
|
||||
class UnmanagedMoveTest extends FileTestBase {
|
||||
|
||||
/**
|
||||
* Move a normal file.
|
||||
*/
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Drupal\KernelTests\Core\File;
|
||||
* @group File
|
||||
*/
|
||||
class UnmanagedSaveDataTest extends FileTestBase {
|
||||
|
||||
/**
|
||||
* Test the file_unmanaged_save_data() function.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\File;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Tests url transform to relative.
|
||||
*
|
||||
* @group Utility
|
||||
*/
|
||||
class UrlTransformRelativeTest extends KernelTestBase {
|
||||
|
||||
public static $modules = ['file_test'];
|
||||
|
||||
/**
|
||||
* Tests file_url_transform_relative function.
|
||||
*
|
||||
* @dataProvider providerFileUrlTransformRelative
|
||||
*/
|
||||
public function testFileUrlTransformRelative($host, $port, $https, $url, $expected) {
|
||||
|
||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
|
||||
$_SERVER['SERVER_PORT'] = $port;
|
||||
$_SERVER['SERVER_SOFTWARE'] = NULL;
|
||||
$_SERVER['SERVER_NAME'] = $host;
|
||||
$_SERVER['REQUEST_URI'] = '/';
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
$_SERVER['SCRIPT_NAME'] = '/index.php';
|
||||
$_SERVER['SCRIPT_FILENAME'] = '/index.php';
|
||||
$_SERVER['PHP_SELF'] = '/index.php';
|
||||
$_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
|
||||
$_SERVER['HTTPS'] = $https;
|
||||
|
||||
$request = Request::createFromGlobals();
|
||||
\Drupal::requestStack()->push($request);
|
||||
|
||||
$this->assertSame($expected, file_url_transform_relative($url));
|
||||
}
|
||||
|
||||
public function providerFileUrlTransformRelative() {
|
||||
$data = [];
|
||||
$data[] = [
|
||||
'example.com',
|
||||
80,
|
||||
'',
|
||||
'http://example.com/page',
|
||||
'/page',
|
||||
];
|
||||
$data[] = [
|
||||
'example.com',
|
||||
443,
|
||||
'on',
|
||||
'https://example.com/page',
|
||||
'/page',
|
||||
];
|
||||
$data[] = [
|
||||
'example.com',
|
||||
8080,
|
||||
'',
|
||||
'https://example.com:8080/page',
|
||||
'/page',
|
||||
];
|
||||
$data[] = [
|
||||
'example.com',
|
||||
8443,
|
||||
'on',
|
||||
'https://example.com:8443/page',
|
||||
'/page',
|
||||
];
|
||||
$data[] = [
|
||||
'example.com',
|
||||
80,
|
||||
'',
|
||||
'http://exampleXcom/page',
|
||||
'http://exampleXcom/page',
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -80,7 +80,8 @@ class FormValidationMessageOrderTest extends KernelTestBase implements FormInter
|
||||
$form_builder = $this->container->get('form_builder');
|
||||
$form_builder->submitForm($this, $form_state);
|
||||
|
||||
$messages = drupal_get_messages();
|
||||
$messages = \Drupal::messenger()->all();
|
||||
\Drupal::messenger()->deleteAll();
|
||||
$this->assertTrue(isset($messages['error']));
|
||||
$error_messages = $messages['error'];
|
||||
$this->assertEqual($error_messages[0], 'Three field is required.');
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Drupal\KernelTests\Core\Image;
|
||||
|
||||
use Drupal\Core\Image\ImageInterface;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Core\Site\Settings;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
@@ -31,7 +31,7 @@ class ToolkitGdTest extends KernelTestBase {
|
||||
protected $white = [255, 255, 255, 0];
|
||||
protected $transparent = [0, 0, 0, 127];
|
||||
// Used as rotate background colors.
|
||||
protected $fuchsia = [255, 0, 255, 0];
|
||||
protected $fuchsia = [255, 0, 255, 0];
|
||||
protected $rotateTransparent = [255, 255, 255, 127];
|
||||
|
||||
protected $width = 40;
|
||||
@@ -119,7 +119,7 @@ class ToolkitGdTest extends KernelTestBase {
|
||||
'gif' => IMAGETYPE_GIF,
|
||||
'jpeg' => IMAGETYPE_JPEG,
|
||||
'jpg' => IMAGETYPE_JPEG,
|
||||
'jpe' => IMAGETYPE_JPEG
|
||||
'jpe' => IMAGETYPE_JPEG,
|
||||
];
|
||||
$image = $this->imageFactory->get();
|
||||
foreach ($expected_image_types as $extension => $expected_image_type) {
|
||||
@@ -266,7 +266,7 @@ class ToolkitGdTest extends KernelTestBase {
|
||||
array_fill(0, 3, 76) + [3 => 0],
|
||||
array_fill(0, 3, 149) + [3 => 0],
|
||||
array_fill(0, 3, 29) + [3 => 0],
|
||||
array_fill(0, 3, 225) + [3 => 127]
|
||||
array_fill(0, 3, 225) + [3 => 127],
|
||||
],
|
||||
],
|
||||
];
|
||||
@@ -282,14 +282,14 @@ class ToolkitGdTest extends KernelTestBase {
|
||||
$image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/' . $file);
|
||||
$toolkit = $image->getToolkit();
|
||||
if (!$image->isValid()) {
|
||||
$this->fail(SafeMarkup::format('Could not load image %file.', ['%file' => $file]));
|
||||
$this->fail(new FormattableMarkup('Could not load image %file.', ['%file' => $file]));
|
||||
continue 2;
|
||||
}
|
||||
$image_original_type = $image->getToolkit()->getType();
|
||||
|
||||
// All images should be converted to truecolor when loaded.
|
||||
$image_truecolor = imageistruecolor($toolkit->getResource());
|
||||
$this->assertTrue($image_truecolor, SafeMarkup::format('Image %file after load is a truecolor image.', ['%file' => $file]));
|
||||
$this->assertTrue($image_truecolor, new FormattableMarkup('Image %file after load is a truecolor image.', ['%file' => $file]));
|
||||
|
||||
// Store the original GD resource.
|
||||
$old_res = $toolkit->getResource();
|
||||
@@ -301,7 +301,7 @@ class ToolkitGdTest extends KernelTestBase {
|
||||
// been destroyed.
|
||||
$new_res = $toolkit->getResource();
|
||||
if ($new_res !== $old_res) {
|
||||
$this->assertFalse(is_resource($old_res), SafeMarkup::format("'%operation' destroyed the original resource.", ['%operation' => $values['function']]));
|
||||
$this->assertFalse(is_resource($old_res), new FormattableMarkup("'%operation' destroyed the original resource.", ['%operation' => $values['function']]));
|
||||
}
|
||||
|
||||
// To keep from flooding the test with assert values, make a general
|
||||
@@ -321,8 +321,8 @@ class ToolkitGdTest extends KernelTestBase {
|
||||
$file_path = $directory . '/' . $op . image_type_to_extension($image->getToolkit()->getType());
|
||||
$image->save($file_path);
|
||||
|
||||
$this->assertTrue($correct_dimensions_real, SafeMarkup::format('Image %file after %action action has proper dimensions.', ['%file' => $file, '%action' => $op]));
|
||||
$this->assertTrue($correct_dimensions_object, SafeMarkup::format('Image %file object after %action action is reporting the proper height and width values.', ['%file' => $file, '%action' => $op]));
|
||||
$this->assertTrue($correct_dimensions_real, new FormattableMarkup('Image %file after %action action has proper dimensions.', ['%file' => $file, '%action' => $op]));
|
||||
$this->assertTrue($correct_dimensions_object, new FormattableMarkup('Image %file object after %action action is reporting the proper height and width values.', ['%file' => $file, '%action' => $op]));
|
||||
|
||||
// JPEG colors will always be messed up due to compression. So we skip
|
||||
// these tests if the original or the result is in jpeg format.
|
||||
@@ -368,7 +368,7 @@ class ToolkitGdTest extends KernelTestBase {
|
||||
// conversion. The convert operation cannot handle that correctly.
|
||||
if ($image->getToolkit()->getType() == $image_original_type || $corner != $this->transparent) {
|
||||
$correct_colors = $this->colorsAreEqual($color, $corner);
|
||||
$this->assertTrue($correct_colors, SafeMarkup::format('Image %file object after %action action has the correct color placement at corner %corner.',
|
||||
$this->assertTrue($correct_colors, new FormattableMarkup('Image %file object after %action action has the correct color placement at corner %corner.',
|
||||
['%file' => $file, '%action' => $op, '%corner' => $key]));
|
||||
}
|
||||
}
|
||||
@@ -386,25 +386,25 @@ class ToolkitGdTest extends KernelTestBase {
|
||||
$image->createNew(50, 20, image_type_to_extension($type, FALSE), '#ffff00');
|
||||
$file = 'from_null' . image_type_to_extension($type);
|
||||
$file_path = $directory . '/' . $file;
|
||||
$this->assertEqual(50, $image->getWidth(), SafeMarkup::format('Image file %file has the correct width.', ['%file' => $file]));
|
||||
$this->assertEqual(20, $image->getHeight(), SafeMarkup::format('Image file %file has the correct height.', ['%file' => $file]));
|
||||
$this->assertEqual(image_type_to_mime_type($type), $image->getMimeType(), SafeMarkup::format('Image file %file has the correct MIME type.', ['%file' => $file]));
|
||||
$this->assertTrue($image->save($file_path), SafeMarkup::format('Image %file created anew from a null image was saved.', ['%file' => $file]));
|
||||
$this->assertEqual(50, $image->getWidth(), new FormattableMarkup('Image file %file has the correct width.', ['%file' => $file]));
|
||||
$this->assertEqual(20, $image->getHeight(), new FormattableMarkup('Image file %file has the correct height.', ['%file' => $file]));
|
||||
$this->assertEqual(image_type_to_mime_type($type), $image->getMimeType(), new FormattableMarkup('Image file %file has the correct MIME type.', ['%file' => $file]));
|
||||
$this->assertTrue($image->save($file_path), new FormattableMarkup('Image %file created anew from a null image was saved.', ['%file' => $file]));
|
||||
|
||||
// Reload saved image.
|
||||
$image_reloaded = $this->imageFactory->get($file_path);
|
||||
if (!$image_reloaded->isValid()) {
|
||||
$this->fail(SafeMarkup::format('Could not load image %file.', ['%file' => $file]));
|
||||
$this->fail(new FormattableMarkup('Could not load image %file.', ['%file' => $file]));
|
||||
continue;
|
||||
}
|
||||
$this->assertEqual(50, $image_reloaded->getWidth(), SafeMarkup::format('Image file %file has the correct width.', ['%file' => $file]));
|
||||
$this->assertEqual(20, $image_reloaded->getHeight(), SafeMarkup::format('Image file %file has the correct height.', ['%file' => $file]));
|
||||
$this->assertEqual(image_type_to_mime_type($type), $image_reloaded->getMimeType(), SafeMarkup::format('Image file %file has the correct MIME type.', ['%file' => $file]));
|
||||
$this->assertEqual(50, $image_reloaded->getWidth(), new FormattableMarkup('Image file %file has the correct width.', ['%file' => $file]));
|
||||
$this->assertEqual(20, $image_reloaded->getHeight(), new FormattableMarkup('Image file %file has the correct height.', ['%file' => $file]));
|
||||
$this->assertEqual(image_type_to_mime_type($type), $image_reloaded->getMimeType(), new FormattableMarkup('Image file %file has the correct MIME type.', ['%file' => $file]));
|
||||
if ($image_reloaded->getToolkit()->getType() == IMAGETYPE_GIF) {
|
||||
$this->assertEqual('#ffff00', $image_reloaded->getToolkit()->getTransparentColor(), SafeMarkup::format('Image file %file has the correct transparent color channel set.', ['%file' => $file]));
|
||||
$this->assertEqual('#ffff00', $image_reloaded->getToolkit()->getTransparentColor(), new FormattableMarkup('Image file %file has the correct transparent color channel set.', ['%file' => $file]));
|
||||
}
|
||||
else {
|
||||
$this->assertEqual(NULL, $image_reloaded->getToolkit()->getTransparentColor(), SafeMarkup::format('Image file %file has no color channel set.', ['%file' => $file]));
|
||||
$this->assertEqual(NULL, $image_reloaded->getToolkit()->getTransparentColor(), new FormattableMarkup('Image file %file has no color channel set.', ['%file' => $file]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -500,12 +500,12 @@ class ToolkitGdTest extends KernelTestBase {
|
||||
$toolkit = $image->getToolkit();
|
||||
|
||||
if (!$image->isValid()) {
|
||||
$this->fail(SafeMarkup::format('Could not load image %file.', ['%file' => $file]));
|
||||
$this->fail(new FormattableMarkup('Could not load image %file.', ['%file' => $file]));
|
||||
}
|
||||
else {
|
||||
// All images should be converted to truecolor when loaded.
|
||||
$image_truecolor = imageistruecolor($toolkit->getResource());
|
||||
$this->assertTrue($image_truecolor, SafeMarkup::format('Image %file after load is a truecolor image.', ['%file' => $file]));
|
||||
$this->assertTrue($image_truecolor, new FormattableMarkup('Image %file after load is a truecolor image.', ['%file' => $file]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -523,7 +523,7 @@ class ToolkitGdTest extends KernelTestBase {
|
||||
// Load up a fresh image.
|
||||
$image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/' . $file);
|
||||
if (!$image->isValid()) {
|
||||
$this->fail(SafeMarkup::format('Could not load image %file.', ['%file' => $file]));
|
||||
$this->fail(new FormattableMarkup('Could not load image %file.', ['%file' => $file]));
|
||||
}
|
||||
|
||||
// Try perform a missing toolkit operation.
|
||||
|
||||
@@ -52,8 +52,8 @@ class InstallerLanguageTest extends KernelTestBase {
|
||||
$info_en = install_profile_info('testing', 'en');
|
||||
$info_nl = install_profile_info('testing', 'nl');
|
||||
|
||||
$this->assertFalse(in_array('locale', $info_en['dependencies']), 'Locale is not set when installing in English.');
|
||||
$this->assertTrue(in_array('locale', $info_nl['dependencies']), 'Locale is set when installing in Dutch.');
|
||||
$this->assertNotContains('locale', $info_en['install'], 'Locale is not set when installing in English.');
|
||||
$this->assertContains('locale', $info_nl['install'], 'Locale is set when installing in Dutch.');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -103,6 +103,11 @@ class MessengerLegacyTest extends KernelTestBase {
|
||||
$this->assertCount(4, $messages[MessengerInterface::TYPE_STATUS]);
|
||||
$this->assertCount(4, $messages[MessengerInterface::TYPE_WARNING]);
|
||||
$this->assertCount(4, $messages[MessengerInterface::TYPE_ERROR]);
|
||||
|
||||
// Test deleteByType().
|
||||
$this->assertCount(4, $messenger->deleteByType(MessengerInterface::TYPE_WARNING));
|
||||
$this->assertCount(0, $messenger->messagesByType(MessengerInterface::TYPE_WARNING));
|
||||
$this->assertCount(4, $messenger->messagesByType(MessengerInterface::TYPE_ERROR));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Plugin\Condition;
|
||||
|
||||
use Drupal\Core\Plugin\Context\Context;
|
||||
use Drupal\Core\Plugin\Context\ContextDefinition;
|
||||
use Drupal\Core\Plugin\Context\EntityContext;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
@@ -66,8 +65,7 @@ class ConditionTestDualUserTest extends KernelTestBase {
|
||||
'user1' => 'anonymous',
|
||||
'user2' => 'anonymous',
|
||||
]);
|
||||
$definition = new ContextDefinition('entity:user');
|
||||
$contexts['anonymous'] = new Context($definition, $this->anonymous);
|
||||
$contexts['anonymous'] = EntityContext::fromEntity($this->anonymous);
|
||||
\Drupal::service('context.handler')->applyContextMapping($condition, $contexts);
|
||||
$this->assertTrue($condition->execute());
|
||||
}
|
||||
@@ -83,9 +81,8 @@ class ConditionTestDualUserTest extends KernelTestBase {
|
||||
'user1' => 'anonymous',
|
||||
'user2' => 'authenticated',
|
||||
]);
|
||||
$definition = new ContextDefinition('entity:user');
|
||||
$contexts['anonymous'] = new Context($definition, $this->anonymous);
|
||||
$contexts['authenticated'] = new Context($definition, $this->authenticated);
|
||||
$contexts['anonymous'] = EntityContext::fromEntity($this->anonymous);
|
||||
$contexts['authenticated'] = EntityContext::fromEntity($this->authenticated);
|
||||
\Drupal::service('context.handler')->applyContextMapping($condition, $contexts);
|
||||
$this->assertFalse($condition->execute());
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Drupal\KernelTests\Core\Plugin\Condition;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
@@ -31,8 +31,8 @@ class CurrentThemeConditionTest extends KernelTestBase {
|
||||
$condition_negated = $manager->createInstance('current_theme');
|
||||
$condition_negated->setConfiguration(['theme' => 'test_theme', 'negate' => TRUE]);
|
||||
|
||||
$this->assertEqual($condition->summary(), SafeMarkup::format('The current theme is @theme', ['@theme' => 'test_theme']));
|
||||
$this->assertEqual($condition_negated->summary(), SafeMarkup::format('The current theme is not @theme', ['@theme' => 'test_theme']));
|
||||
$this->assertEqual($condition->summary(), new FormattableMarkup('The current theme is @theme', ['@theme' => 'test_theme']));
|
||||
$this->assertEqual($condition_negated->summary(), new FormattableMarkup('The current theme is not @theme', ['@theme' => 'test_theme']));
|
||||
|
||||
// The expected theme has not been set up yet.
|
||||
$this->assertFalse($condition->execute());
|
||||
|
||||
+4
-4
@@ -3,7 +3,8 @@
|
||||
namespace Drupal\KernelTests\Core\Plugin\Condition;
|
||||
|
||||
use Drupal\Core\Plugin\Context\Context;
|
||||
use Drupal\Core\Plugin\Context\ContextDefinition;
|
||||
use Drupal\Core\Plugin\Context\EntityContext;
|
||||
use Drupal\Core\Plugin\Context\EntityContextDefinition;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
@@ -44,7 +45,7 @@ class OptionalContextConditionTest extends KernelTestBase {
|
||||
->setContextMapping([
|
||||
'node' => 'node',
|
||||
]);
|
||||
$definition = new ContextDefinition('entity:node');
|
||||
$definition = EntityContextDefinition::fromEntityTypeId('node');
|
||||
$contexts['node'] = (new Context($definition));
|
||||
\Drupal::service('context.handler')->applyContextMapping($condition, $contexts);
|
||||
$this->assertTrue($condition->execute());
|
||||
@@ -61,9 +62,8 @@ class OptionalContextConditionTest extends KernelTestBase {
|
||||
->setContextMapping([
|
||||
'node' => 'node',
|
||||
]);
|
||||
$definition = new ContextDefinition('entity:node');
|
||||
$node = Node::create(['type' => 'example']);
|
||||
$contexts['node'] = new Context($definition, $node);
|
||||
$contexts['node'] = EntityContext::fromEntity($node);
|
||||
\Drupal::service('context.handler')->applyContextMapping($condition, $contexts);
|
||||
$this->assertFalse($condition->execute());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Plugin;
|
||||
|
||||
use Drupal\Core\Plugin\Context\ContextDefinition;
|
||||
use Drupal\Core\Plugin\Context\EntityContext;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\Core\Plugin\Context\ContextDefinition
|
||||
* @group Plugin
|
||||
*/
|
||||
class ContextDefinitionTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['entity_test', 'user'];
|
||||
|
||||
/**
|
||||
* @covers ::isSatisfiedBy
|
||||
*/
|
||||
public function testIsSatisfiedBy() {
|
||||
$this->installEntitySchema('user');
|
||||
|
||||
$value = EntityTest::create([]);
|
||||
// Assert that the entity has at least one violation.
|
||||
$this->assertNotEmpty($value->validate());
|
||||
// Assert that these violations do not prevent it from satisfying the
|
||||
// requirements of another object.
|
||||
$requirement = new ContextDefinition('any');
|
||||
$context = EntityContext::fromEntity($value);
|
||||
$this->assertTrue($requirement->isSatisfiedBy($context));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Drupal\KernelTests\Core\Plugin;
|
||||
|
||||
use Drupal\Component\Plugin\Exception\ContextException;
|
||||
use Drupal\Core\Plugin\Context\ContextDefinition;
|
||||
use Drupal\Core\Plugin\Context\EntityContextDefinition;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
@@ -45,7 +45,7 @@ class ContextPluginTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
// Test the getContextDefinitions() method.
|
||||
$user_context_definition = ContextDefinition::create('entity:user')->setLabel(t('User'));
|
||||
$user_context_definition = EntityContextDefinition::fromEntityTypeId('user')->setLabel(t('User'));
|
||||
$this->assertEqual($plugin->getContextDefinitions()['user']->getLabel(), $user_context_definition->getLabel());
|
||||
|
||||
// Test the getContextDefinition() method for a valid context.
|
||||
|
||||
@@ -9,6 +9,11 @@ namespace Drupal\KernelTests\Core\Plugin;
|
||||
*/
|
||||
class DerivativeTest extends PluginTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['node', 'user'];
|
||||
|
||||
/**
|
||||
* Tests getDefinitions() and getDefinition() with a derivativeDecorator.
|
||||
*/
|
||||
|
||||
@@ -73,8 +73,8 @@ class AnnotatedClassDiscoveryTest extends DiscoveryTestBase {
|
||||
],
|
||||
];
|
||||
|
||||
$base_directory = \Drupal::root() . '/core/modules/system/tests/modules/plugin_test/src';
|
||||
$base_directory2 = \Drupal::root() . '/core/modules/system/tests/modules/plugin_test_extended/src';
|
||||
$base_directory = $this->root . '/core/modules/system/tests/modules/plugin_test/src';
|
||||
$base_directory2 = $this->root . '/core/modules/system/tests/modules/plugin_test_extended/src';
|
||||
$namespaces = new \ArrayObject(['Drupal\plugin_test' => $base_directory, 'Drupal\plugin_test_extended' => $base_directory2]);
|
||||
|
||||
$annotation_namespaces = ['Drupal\plugin_test\Plugin\Annotation', 'Drupal\plugin_test_extended\Plugin\Annotation'];
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ class CustomAnnotationClassDiscoveryTest extends DiscoveryTestBase {
|
||||
],
|
||||
];
|
||||
|
||||
$base_directory = \Drupal::root() . '/core/modules/system/tests/modules/plugin_test/src';
|
||||
$base_directory = $this->root . '/core/modules/system/tests/modules/plugin_test/src';
|
||||
$root_namespaces = new \ArrayObject(['Drupal\plugin_test' => $base_directory]);
|
||||
|
||||
$this->discovery = new AnnotatedClassDiscovery('Plugin/plugin_test/custom_annotation', $root_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample');
|
||||
|
||||
+1
-1
@@ -80,7 +80,7 @@ class CustomDirectoryAnnotatedClassDiscoveryTest extends DiscoveryTestBase {
|
||||
],
|
||||
];
|
||||
|
||||
$base_directory = \Drupal::root() . '/core/modules/system/tests/modules/plugin_test/src';
|
||||
$base_directory = $this->root . '/core/modules/system/tests/modules/plugin_test/src';
|
||||
$namespaces = new \ArrayObject(['Drupal\plugin_test' => $base_directory]);
|
||||
|
||||
$this->discovery = new AnnotatedClassDiscovery('', $namespaces);
|
||||
|
||||
@@ -11,6 +11,11 @@ use Drupal\Component\Plugin\Exception\ExceptionInterface;
|
||||
*/
|
||||
class FactoryTest extends PluginTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['node', 'user'];
|
||||
|
||||
/**
|
||||
* Test that DefaultFactory can create a plugin instance.
|
||||
*/
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user