Update to Drupal 8.1.8. For more information, see https://www.drupal.org/project/drupal/releases/8.1.8
This commit is contained in:
parent
e9f047ccf8
commit
f9f23cdf38
312 changed files with 6751 additions and 1546 deletions
|
@ -7,6 +7,7 @@ use Behat\Mink\Element\Element;
|
|||
use Behat\Mink\Mink;
|
||||
use Behat\Mink\Session;
|
||||
use Drupal\Component\FileCache\FileCacheFactory;
|
||||
use Drupal\Component\Serialization\Json;
|
||||
use Drupal\Component\Serialization\Yaml;
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
|
@ -27,8 +28,7 @@ use Drupal\simpletest\AssertHelperTrait;
|
|||
use Drupal\simpletest\ContentTypeCreationTrait;
|
||||
use Drupal\simpletest\BlockCreationTrait;
|
||||
use Drupal\simpletest\NodeCreationTrait;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\simpletest\UserCreationTrait;
|
||||
use Symfony\Component\CssSelector\CssSelectorConverter;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
|
@ -56,6 +56,10 @@ abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase {
|
|||
use ContentTypeCreationTrait {
|
||||
createContentType as drupalCreateContentType;
|
||||
}
|
||||
use UserCreationTrait {
|
||||
createRole as drupalCreateRole;
|
||||
createUser as drupalCreateUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class loader.
|
||||
|
@ -683,139 +687,6 @@ abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase {
|
|||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a user with a given set of permissions.
|
||||
*
|
||||
* @param array $permissions
|
||||
* (optional) Array of permission names to assign to user. Note that the
|
||||
* user always has the default permissions derived from the
|
||||
* "authenticated users" role.
|
||||
* @param string $name
|
||||
* (optional) The user name.
|
||||
*
|
||||
* @return \Drupal\user\Entity\User|false
|
||||
* A fully loaded user object with passRaw property, or FALSE if account
|
||||
* creation fails.
|
||||
*/
|
||||
protected function drupalCreateUser(array $permissions = array(), $name = NULL) {
|
||||
// Create a role with the given permission set, if any.
|
||||
$rid = FALSE;
|
||||
if ($permissions) {
|
||||
$rid = $this->drupalCreateRole($permissions);
|
||||
if (!$rid) {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
// Create a user assigned to that role.
|
||||
$edit = array();
|
||||
$edit['name'] = !empty($name) ? $name : $this->randomMachineName();
|
||||
$edit['mail'] = $edit['name'] . '@example.com';
|
||||
$edit['pass'] = user_password();
|
||||
$edit['status'] = 1;
|
||||
if ($rid) {
|
||||
$edit['roles'] = array($rid);
|
||||
}
|
||||
|
||||
$account = User::create($edit);
|
||||
$account->save();
|
||||
|
||||
$this->assertNotNull($account->id(), SafeMarkup::format('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])));
|
||||
if (!$account->id()) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Add the raw password so that we can log in as this user.
|
||||
$account->passRaw = $edit['pass'];
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a role with specified permissions.
|
||||
*
|
||||
* @param array $permissions
|
||||
* Array of permission names to assign to role.
|
||||
* @param string $rid
|
||||
* (optional) The role ID (machine name). Defaults to a random name.
|
||||
* @param string $name
|
||||
* (optional) The label for the role. Defaults to a random string.
|
||||
* @param int $weight
|
||||
* (optional) The weight for the role. Defaults NULL so that entity_create()
|
||||
* sets the weight to maximum + 1.
|
||||
*
|
||||
* @return string
|
||||
* Role ID of newly created role, or FALSE if role creation failed.
|
||||
*/
|
||||
protected function drupalCreateRole(array $permissions, $rid = NULL, $name = NULL, $weight = NULL) {
|
||||
// Generate a random, lowercase machine name if none was passed.
|
||||
if (!isset($rid)) {
|
||||
$rid = strtolower($this->randomMachineName(8));
|
||||
}
|
||||
// Generate a random label.
|
||||
if (!isset($name)) {
|
||||
// In the role UI role names are trimmed and random string can start or
|
||||
// end with a space.
|
||||
$name = trim($this->randomString(8));
|
||||
}
|
||||
|
||||
// Check the all the permissions strings are valid.
|
||||
if (!$this->checkPermissions($permissions)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Create new role.
|
||||
/* @var \Drupal\user\RoleInterface $role */
|
||||
$role = Role::create(array(
|
||||
'id' => $rid,
|
||||
'label' => $name,
|
||||
));
|
||||
if (!is_null($weight)) {
|
||||
$role->set('weight', $weight);
|
||||
}
|
||||
$result = $role->save();
|
||||
|
||||
$this->assertSame($result, SAVED_NEW, SafeMarkup::format('Created role ID @rid with name @name.', array(
|
||||
'@name' => var_export($role->label(), TRUE),
|
||||
'@rid' => var_export($role->id(), TRUE),
|
||||
)));
|
||||
|
||||
if ($result === SAVED_NEW) {
|
||||
// Grant the specified permissions to the role, if any.
|
||||
if (!empty($permissions)) {
|
||||
user_role_grant_permissions($role->id(), $permissions);
|
||||
$assigned_permissions = entity_load('user_role', $role->id())->getPermissions();
|
||||
$missing_permissions = array_diff($permissions, $assigned_permissions);
|
||||
if ($missing_permissions) {
|
||||
$this->fail(SafeMarkup::format('Failed to create permissions: @perms', array('@perms' => implode(', ', $missing_permissions))));
|
||||
}
|
||||
}
|
||||
return $role->id();
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a given list of permission names is valid.
|
||||
*
|
||||
* @param array $permissions
|
||||
* The permission names to check.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the permissions are valid, FALSE otherwise.
|
||||
*/
|
||||
protected function checkPermissions(array $permissions) {
|
||||
$available = array_keys(\Drupal::service('user.permissions')->getPermissions());
|
||||
$valid = TRUE;
|
||||
foreach ($permissions as $permission) {
|
||||
if (!in_array($permission, $available)) {
|
||||
$this->fail(SafeMarkup::format('Invalid permission %permission.', array('%permission' => $permission)));
|
||||
$valid = FALSE;
|
||||
}
|
||||
}
|
||||
return $valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs in a user using the Mink controlled browser.
|
||||
*
|
||||
|
@ -1767,6 +1638,20 @@ abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase {
|
|||
return $this->getSession()->getCurrentUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the JavaScript drupalSettings variable for the currently-loaded page.
|
||||
*
|
||||
* @return array
|
||||
* The JSON decoded drupalSettings value from the current page.
|
||||
*/
|
||||
protected function getDrupalSettings() {
|
||||
$html = $this->getSession()->getPage()->getHtml();
|
||||
if (preg_match('@<script type="application/json" data-drupal-selector="drupal-settings-json">([^<]*)</script>@', $html, $matches)) {
|
||||
return Json::decode($matches[1]);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
@ -321,4 +321,72 @@ class HtmlTest extends UnitTestCase {
|
|||
$this->assertSame('', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::transformRootRelativeUrlsToAbsolute
|
||||
* @dataProvider providerTestTransformRootRelativeUrlsToAbsolute
|
||||
*/
|
||||
public function testTransformRootRelativeUrlsToAbsolute($html, $scheme_and_host, $expected_html) {
|
||||
$this->assertSame($expected_html ?: $html, Html::transformRootRelativeUrlsToAbsolute($html, $scheme_and_host));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::transformRootRelativeUrlsToAbsolute
|
||||
* @dataProvider providerTestTransformRootRelativeUrlsToAbsoluteAssertion
|
||||
* @expectedException \AssertionError
|
||||
*/
|
||||
public function testTransformRootRelativeUrlsToAbsoluteAssertion($scheme_and_host) {
|
||||
Html::transformRootRelativeUrlsToAbsolute('', $scheme_and_host);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for testTransformRootRelativeUrlsToAbsolute().
|
||||
*
|
||||
* @return array
|
||||
* Test data.
|
||||
*/
|
||||
public function providerTestTransformRootRelativeUrlsToAbsolute() {
|
||||
$data = [];
|
||||
|
||||
// One random tag name.
|
||||
$tag_name = strtolower($this->randomMachineName());
|
||||
|
||||
// A site installed either in the root of a domain or a subdirectory.
|
||||
$base_paths = ['/', '/subdir/' . $this->randomMachineName() . '/'];
|
||||
|
||||
foreach ($base_paths as $base_path) {
|
||||
// The only attribute that has more than just a URL as its value, is
|
||||
// 'srcset', so special-case it.
|
||||
$data += [
|
||||
"$tag_name, srcset, $base_path: root-relative" => ["<$tag_name srcset=\"http://example.com{$base_path}already-absolute 200w, {$base_path}root-relative 300w\">root-relative test</$tag_name>", 'http://example.com', "<$tag_name srcset=\"http://example.com{$base_path}already-absolute 200w, http://example.com{$base_path}root-relative 300w\">root-relative test</$tag_name>"],
|
||||
"$tag_name, srcset, $base_path: protocol-relative" => ["<$tag_name srcset=\"http://example.com{$base_path}already-absolute 200w, //example.com{$base_path}protocol-relative 300w\">protocol-relative test</$tag_name>", 'http://example.com', FALSE],
|
||||
"$tag_name, srcset, $base_path: absolute" => ["<$tag_name srcset=\"http://example.com{$base_path}already-absolute 200w, http://example.com{$base_path}absolute 300w\">absolute test</$tag_name>", 'http://example.com', FALSE],
|
||||
];
|
||||
|
||||
foreach (['href', 'poster', 'src', 'cite', 'data', 'action', 'formaction', 'about'] as $attribute) {
|
||||
$data += [
|
||||
"$tag_name, $attribute, $base_path: root-relative" => ["<$tag_name $attribute=\"{$base_path}root-relative\">root-relative test</$tag_name>", 'http://example.com', "<$tag_name $attribute=\"http://example.com{$base_path}root-relative\">root-relative test</$tag_name>"],
|
||||
"$tag_name, $attribute, $base_path: protocol-relative" => ["<$tag_name $attribute=\"//example.com{$base_path}protocol-relative\">protocol-relative test</$tag_name>", 'http://example.com', FALSE],
|
||||
"$tag_name, $attribute, $base_path: absolute" => ["<$tag_name $attribute=\"http://example.com{$base_path}absolute\">absolute test</$tag_name>", 'http://example.com', FALSE],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for testTransformRootRelativeUrlsToAbsoluteAssertion().
|
||||
*
|
||||
* @return array
|
||||
* Test data.
|
||||
*/
|
||||
public function providerTestTransformRootRelativeUrlsToAbsoluteAssertion() {
|
||||
return [
|
||||
'only relative path' => ['llama'],
|
||||
'only root-relative path' => ['/llama'],
|
||||
'host and path' => ['example.com/llama'],
|
||||
'scheme, host and path' => ['http://example.com/llama'],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -64,6 +64,7 @@ class ComposerIntegrationTest extends UnitTestCase {
|
|||
$this->root . '/core/lib/Drupal/Component/Serialization',
|
||||
$this->root . '/core/lib/Drupal/Component/Transliteration',
|
||||
$this->root . '/core/lib/Drupal/Component/Utility',
|
||||
$this->root . '/core/lib/Drupal/Component/Uuid',
|
||||
];
|
||||
}
|
||||
|
||||
|
|
173
core/tests/Drupal/Tests/Core/Assert/AssertLegacyTraitTest.php
Normal file
173
core/tests/Drupal/Tests/Core/Assert/AssertLegacyTraitTest.php
Normal file
|
@ -0,0 +1,173 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\Core\Assert;
|
||||
|
||||
use Behat\Mink\Element\DocumentElement;
|
||||
use Behat\Mink\Element\NodeElement;
|
||||
use Behat\Mink\Session;
|
||||
use Drupal\Component\Render\MarkupInterface;
|
||||
use Drupal\FunctionalTests\AssertLegacyTrait;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\Tests\WebAssert;
|
||||
use PHPUnit_Framework_ExpectationFailedException;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\FunctionalTests\AssertLegacyTrait
|
||||
* @group Assert
|
||||
*/
|
||||
class AssertLegacyTraitTest extends UnitTestCase {
|
||||
|
||||
use AssertLegacyTrait;
|
||||
|
||||
/**
|
||||
* The mocked Mink session object used for testing.
|
||||
*
|
||||
* @var \Behat\Mink\Session|\Prophecy\Prophecy\ObjectProphecy
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* The mocked page element used for testing.
|
||||
*
|
||||
* @var Behat\Mink\Element\DocumentElement|\Prophecy\Prophecy\ObjectProphecy
|
||||
*/
|
||||
protected $page;
|
||||
|
||||
/**
|
||||
* The mocked web assert class.
|
||||
*
|
||||
* @var \Drupal\Tests\WebAssert|\Prophecy\Prophecy\ObjectProphecy
|
||||
*/
|
||||
protected $webAssert;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->page = $this->prophesize(DocumentElement::class);
|
||||
$this->session = $this->prophesize(Session::class);
|
||||
$this->session->getPage()->willReturn($this->page->reveal());
|
||||
$this->webAssert = $this->prophesize(WebAssert::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::assertUniqueText
|
||||
*/
|
||||
public function testAssertUniqueText() {
|
||||
$this->page->getText()->willReturn('foo bar bar');
|
||||
$this->assertUniqueText('foo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::assertUniqueText
|
||||
*/
|
||||
public function testAssertUniqueTextFail() {
|
||||
$this->page->getText()->willReturn('foo bar bar');
|
||||
$this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
|
||||
$this->assertUniqueText('bar');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::assertUniqueText
|
||||
*/
|
||||
public function testAssertUniqueTextUnknown() {
|
||||
$this->page->getText()->willReturn('foo bar bar');
|
||||
$this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
|
||||
$this->assertUniqueText('alice');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::assertUniqueText
|
||||
*/
|
||||
public function testAssertUniqueTextMarkup() {
|
||||
$this->page->getText()->willReturn('foo bar bar');
|
||||
$markupObject = $this->prophesize(MarkupInterface::class);
|
||||
$markupObject->__toString()->willReturn('foo');
|
||||
$this->assertUniqueText($markupObject->reveal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::assertNoUniqueText
|
||||
*/
|
||||
public function testAssertNoUniqueText() {
|
||||
$this->page->getText()->willReturn('foo bar bar');
|
||||
$this->assertNoUniqueText('bar');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::assertNoUniqueText
|
||||
*/
|
||||
public function testAssertNoUniqueTextFail() {
|
||||
$this->page->getText()->willReturn('foo bar bar');
|
||||
$this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
|
||||
$this->assertNoUniqueText('foo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::assertNoUniqueText
|
||||
*/
|
||||
public function testAssertNoUniqueTextUnknown() {
|
||||
$this->page->getText()->willReturn('foo bar bar');
|
||||
$this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
|
||||
$this->assertNoUniqueText('alice');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::assertNoUniqueText
|
||||
*/
|
||||
public function testAssertNoUniqueTextMarkup() {
|
||||
$this->page->getText()->willReturn('foo bar bar');
|
||||
$markupObject = $this->prophesize(MarkupInterface::class);
|
||||
$markupObject->__toString()->willReturn('bar');
|
||||
$this->assertNoUniqueText($markupObject->reveal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::assertOptionSelected
|
||||
*/
|
||||
public function testAssertOptionSelected() {
|
||||
$option_field = $this->prophesize(NodeElement::class);
|
||||
$option_field->hasAttribute('selected')->willReturn(TRUE);
|
||||
|
||||
$this->webAssert
|
||||
->optionExists('myselect', 'two')
|
||||
->willReturn($option_field->reveal());
|
||||
|
||||
$this->assertOptionSelected('myselect', 'two');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::assertOptionSelected
|
||||
*/
|
||||
public function testAssertOptionSelectedFail() {
|
||||
$option_field = $this->prophesize(NodeElement::class);
|
||||
$option_field->hasAttribute('selected')->willReturn(FALSE);
|
||||
|
||||
$this->webAssert
|
||||
->optionExists('myselect', 'two')
|
||||
->willReturn($option_field->reveal());
|
||||
|
||||
$this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
|
||||
$this->assertOptionSelected('myselect', 'two');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a mocked behat session object.
|
||||
*
|
||||
* @return \Behat\Mink\Session
|
||||
* The mocked session.
|
||||
*/
|
||||
protected function getSession() {
|
||||
return $this->session->reveal();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function assertSession($name = NULL) {
|
||||
return $this->webAssert->reveal();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\Core\Config;
|
||||
|
||||
use Drupal\Core\Config\ConfigCollectionInfo;
|
||||
use Drupal\Core\Config\ConfigCrudEvent;
|
||||
use Drupal\Core\Config\ConfigFactoryOverrideBase;
|
||||
use Drupal\Core\Config\ConfigRenameEvent;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\Core\Config\ConfigFactoryOverrideBase
|
||||
* @group config
|
||||
*/
|
||||
class ConfigFactoryOverrideBaseTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestFilterNestedArray
|
||||
*/
|
||||
public function testFilterNestedArray(array $original_data, array $override_data_before, array $override_data_after, $changed) {
|
||||
$config_factory = new TestConfigFactoryOverrideBase();
|
||||
$result = $config_factory->doFilterNestedArray($original_data, $override_data_before);
|
||||
$this->assertEquals($changed, $result);
|
||||
$this->assertEquals($override_data_after, $override_data_before);
|
||||
}
|
||||
|
||||
public function providerTestFilterNestedArray() {
|
||||
$data = [];
|
||||
$data['empty'] = [
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
FALSE,
|
||||
];
|
||||
|
||||
$data['one-level-no-change'] = [
|
||||
['key' => 'value'],
|
||||
[],
|
||||
[],
|
||||
FALSE,
|
||||
];
|
||||
|
||||
$data['one-level-override-no-change'] = [
|
||||
['key' => 'value'],
|
||||
['key' => 'value2'],
|
||||
['key' => 'value2'],
|
||||
FALSE,
|
||||
];
|
||||
|
||||
$data['one-level-override-change'] = [
|
||||
['key' => 'value'],
|
||||
['key2' => 'value2'],
|
||||
[],
|
||||
TRUE,
|
||||
];
|
||||
|
||||
$data['one-level-multiple-override-change'] = [
|
||||
['key' => 'value', 'key2' => 'value2'],
|
||||
['key2' => 'value2', 'key3' => 'value3'],
|
||||
['key2' => 'value2'],
|
||||
TRUE,
|
||||
];
|
||||
|
||||
$data['multiple-level-multiple-override-change'] = [
|
||||
['key' => ['key' => 'value'], 'key2' => ['key' => 'value']],
|
||||
['key' => ['key2' => 'value2'], 'key2' => ['key' => 'value']],
|
||||
['key2' => ['key' => 'value']],
|
||||
TRUE,
|
||||
];
|
||||
|
||||
$data['original-scalar-array-override'] = [
|
||||
['key' => 'value'],
|
||||
['key' => ['value1', 'value2']],
|
||||
[],
|
||||
TRUE,
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class TestConfigFactoryOverrideBase extends ConfigFactoryOverrideBase {
|
||||
|
||||
public function doFilterNestedArray(array $original_data, array &$override_data) {
|
||||
return $this->filterNestedArray($original_data, $override_data);
|
||||
}
|
||||
|
||||
public function addCollections(ConfigCollectionInfo $collection_info) {
|
||||
}
|
||||
|
||||
public function onConfigSave(ConfigCrudEvent $event) {
|
||||
}
|
||||
|
||||
public function onConfigDelete(ConfigCrudEvent $event) {
|
||||
}
|
||||
|
||||
public function onConfigRename(ConfigRenameEvent $event) {
|
||||
}
|
||||
|
||||
}
|
154
core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php
Normal file
154
core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php
Normal file
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\Core\Database;
|
||||
|
||||
use Drupal\Core\Database\Database;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\Core\Database\Database
|
||||
*
|
||||
* @group Database
|
||||
*/
|
||||
class UrlConversionTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @covers ::convertDbUrlToConnectionInfo
|
||||
*
|
||||
* @dataProvider providerConvertDbUrlToConnectionInfo
|
||||
*/
|
||||
public function testDbUrltoConnectionConversion($root, $url, $database_array) {
|
||||
$result = Database::convertDbUrlToConnectionInfo($url, $root);
|
||||
$this->assertEquals($database_array, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dataprovider for testDbUrltoConnectionConversion().
|
||||
*
|
||||
* @return array
|
||||
* Array of arrays with the following elements:
|
||||
* - root: The baseroot string, only used with sqlite drivers.
|
||||
* - url: The full URL string to be tested.
|
||||
* - database_array: An array containing the expected results.
|
||||
*/
|
||||
public function providerConvertDbUrlToConnectionInfo() {
|
||||
// Some valid datasets.
|
||||
$root1 = '';
|
||||
$url1 = 'mysql://test_user:test_pass@test_host:3306/test_database';
|
||||
$database_array1 = [
|
||||
'driver' => 'mysql',
|
||||
'username' => 'test_user',
|
||||
'password' => 'test_pass',
|
||||
'host' => 'test_host',
|
||||
'database' => 'test_database',
|
||||
'port' => '3306',
|
||||
];
|
||||
$root2 = '/var/www/d8';
|
||||
$url2 = 'sqlite://test_user:test_pass@test_host:3306/test_database';
|
||||
$database_array2 = [
|
||||
'driver' => 'sqlite',
|
||||
'username' => 'test_user',
|
||||
'password' => 'test_pass',
|
||||
'host' => 'test_host',
|
||||
'database' => $root2 . '/test_database',
|
||||
'port' => 3306,
|
||||
];
|
||||
return [
|
||||
[$root1, $url1, $database_array1],
|
||||
[$root2, $url2, $database_array2],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test ::convertDbUrlToConnectionInfo() exception for invalid arguments.
|
||||
*
|
||||
* @dataProvider providerInvalidArgumentsUrlConversion
|
||||
*/
|
||||
public function testGetInvalidArgumentExceptionInUrlConversion($url, $root) {
|
||||
$this->setExpectedException(\InvalidArgumentException::class);
|
||||
Database::convertDbUrlToConnectionInfo($url, $root);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dataprovider for testGetInvalidArgumentExceptionInUrlConversion().
|
||||
*
|
||||
* @return array
|
||||
* Array of arrays with the following elements:
|
||||
* - An invalid Url string.
|
||||
* - Drupal root string.
|
||||
*/
|
||||
public function providerInvalidArgumentsUrlConversion() {
|
||||
return [
|
||||
['foo', ''],
|
||||
['foo', 'bar'],
|
||||
['foo://', 'bar'],
|
||||
['foo://bar', 'baz'],
|
||||
['foo://bar:port', 'baz'],
|
||||
['foo/bar/baz', 'bar2'],
|
||||
['foo://bar:baz@test1', 'test2'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::convertDbUrlToConnectionInfo
|
||||
*
|
||||
* @dataProvider providerGetConnectionInfoAsUrl
|
||||
*/
|
||||
public function testGetConnectionInfoAsUrl(Array $info, $expected_url) {
|
||||
|
||||
Database::addConnectionInfo('default', 'default', $info);
|
||||
$url = Database::getConnectionInfoAsUrl();
|
||||
|
||||
// Remove the connection to not pollute subsequent datasets being tested.
|
||||
Database::removeConnection('default');
|
||||
|
||||
$this->assertEquals($expected_url, $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dataprovider for testGetConnectionInfoAsUrl().
|
||||
*
|
||||
* @return array
|
||||
* Array of arrays with the following elements:
|
||||
* - An array mocking the database connection info. Possible keys are
|
||||
* database, username, password, prefix, host, port, namespace and driver.
|
||||
* - The expected URL after conversion.
|
||||
*/
|
||||
public function providerGetConnectionInfoAsUrl() {
|
||||
$info1 = [
|
||||
'database' => 'test_database',
|
||||
'username' => 'test_user',
|
||||
'password' => 'test_pass',
|
||||
'prefix' => '',
|
||||
'host' => 'test_host',
|
||||
'port' => '3306',
|
||||
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
|
||||
'driver' => 'mysql',
|
||||
];
|
||||
$expected_url1 = 'mysql://test_user:test_pass@test_host:3306/test_database';
|
||||
|
||||
$info2 = [
|
||||
'database' => 'test_database',
|
||||
'username' => 'test_user',
|
||||
'password' => 'test_pass',
|
||||
'prefix' => 'pre',
|
||||
'host' => 'test_host',
|
||||
'port' => '3306',
|
||||
'driver' => 'mysql',
|
||||
];
|
||||
$expected_url2 = 'mysql://test_user:test_pass@test_host:3306/test_database#pre';
|
||||
|
||||
$info3 = [
|
||||
'database' => 'test_database',
|
||||
'driver' => 'sqlite',
|
||||
];
|
||||
$expected_url3 = 'sqlite://localhost/test_database';
|
||||
|
||||
return [
|
||||
[$info1, $expected_url1],
|
||||
[$info2, $expected_url2],
|
||||
[$info3, $expected_url3],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
|
@ -384,6 +384,38 @@ class EntityResolverManagerTest extends UnitTestCase {
|
|||
$this->assertEquals(array('entity_test' => array('type' => 'entity:entity_test')), $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests an _entity_form route where a non-entity parameter is first.
|
||||
*
|
||||
* The {argument} preceding {entity_test} in route path, is upcasting with a
|
||||
* custom param converter.
|
||||
*
|
||||
* @covers ::setRouteOptions
|
||||
* @covers ::getControllerClass
|
||||
* @covers ::getEntityTypes
|
||||
* @covers ::setParametersFromReflection
|
||||
* @covers ::setParametersFromEntityInformation
|
||||
*/
|
||||
public function testSetRouteOptionsWithEntityFormRouteAndArgument() {
|
||||
$this->setupEntityTypes();
|
||||
$route = new Route('/example/{argument}/{entity_test}', [
|
||||
'_entity_form' => 'entity_test.edit',
|
||||
]);
|
||||
// Add {argument} parameter configuration. In this case {argument} is
|
||||
// upcasted by a custom param converter 'argument_type'.
|
||||
$route->setOption('parameters', ['argument' => ['type' => 'argument_type']]);
|
||||
|
||||
$defaults = $route->getDefaults();
|
||||
$this->entityResolverManager->setRouteOptions($route);
|
||||
$this->assertEquals($defaults, $route->getDefaults());
|
||||
$parameters = $route->getOption('parameters');
|
||||
$expect = [
|
||||
'argument' => ['type' => 'argument_type'],
|
||||
'entity_test' => ['type' => 'entity:entity_test'],
|
||||
];
|
||||
$this->assertEquals($expect, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the entity manager mock returning entity type objects.
|
||||
*/
|
||||
|
|
|
@ -76,6 +76,20 @@ class EntityUrlTest extends UnitTestCase {
|
|||
*/
|
||||
const NON_DEFAULT_REVISION = FALSE;
|
||||
|
||||
/**
|
||||
* Indicator that the test entity type has no bundle key.
|
||||
*
|
||||
* @var false
|
||||
*/
|
||||
const HAS_NO_BUNDLE_KEY = FALSE;
|
||||
|
||||
/**
|
||||
* Indicator that the test entity type has a bundle key.
|
||||
*
|
||||
* @var true
|
||||
*/
|
||||
const HAS_BUNDLE_KEY = TRUE;
|
||||
|
||||
/**
|
||||
* Tests the toUrl() method without an entity ID.
|
||||
*
|
||||
|
@ -185,20 +199,94 @@ class EntityUrlTest extends UnitTestCase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Tests the toUrl() method with the 'collection' link template.
|
||||
* Tests the toUrl() method with link templates without an entity ID.
|
||||
*
|
||||
* @param string $link_template
|
||||
* The link template to test.
|
||||
* @param string $expected_route_name
|
||||
* The expected route name of the generated URL.
|
||||
*
|
||||
* @dataProvider providerTestToUrlLinkTemplateNoId
|
||||
*
|
||||
* @covers ::toUrl
|
||||
* @covers ::linkTemplates
|
||||
* @covers ::urlRouteParameters
|
||||
*/
|
||||
public function testToUrlLinkTemplateCollection() {
|
||||
public function testToUrlLinkTemplateNoId($link_template, $expected_route_name) {
|
||||
$entity = $this->getEntity(Entity::class, ['id' => $this->entityId]);
|
||||
$link_template = 'collection';
|
||||
$this->registerLinkTemplate($link_template);
|
||||
|
||||
/** @var \Drupal\Core\Url $url */
|
||||
$url = $entity->toUrl($link_template);
|
||||
$this->assertUrl('entity.test_entity.collection', [], $entity, FALSE, $url);
|
||||
$this->assertUrl($expected_route_name, [], $entity, FALSE, $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides data for testToUrlLinkTemplateNoId().
|
||||
*
|
||||
* @return array
|
||||
* An array of test cases for testToUrlLinkTemplateNoId().
|
||||
*/
|
||||
public function providerTestToUrlLinkTemplateNoId() {
|
||||
$test_cases = [];
|
||||
|
||||
$test_cases['collection'] = ['collection', 'entity.test_entity.collection'];
|
||||
$test_cases['add-page'] = ['add-page', 'entity.test_entity.add_page'];
|
||||
|
||||
return $test_cases;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the toUrl() method with the 'revision' link template.
|
||||
*
|
||||
* @param bool $has_bundle_key
|
||||
* Whether or not the mock entity type should have a bundle key.
|
||||
* @param string|null $bundle_entity_type
|
||||
* The ID of the bundle entity type of the mock entity type, or NULL if the
|
||||
* mock entity type should not have a bundle entity type.
|
||||
* @param string $bundle_key
|
||||
* The bundle key of the mock entity type or FALSE if the entity type should
|
||||
* not have a bundle key.
|
||||
* @param array $expected_route_parameters
|
||||
* The expected route parameters of the generated URL.
|
||||
*
|
||||
* @dataProvider providerTestToUrlLinkTemplateAddForm
|
||||
*
|
||||
* @covers ::toUrl
|
||||
* @covers ::linkTemplates
|
||||
* @covers ::urlRouteParameters
|
||||
*/
|
||||
public function testToUrlLinkTemplateAddForm($has_bundle_key, $bundle_entity_type, $bundle_key, $expected_route_parameters) {
|
||||
$values = ['id' => $this->entityId, 'langcode' => $this->langcode];
|
||||
$entity = $this->getEntity(Entity::class, $values);
|
||||
$this->entityType->hasKey('bundle')->willReturn($has_bundle_key);
|
||||
$this->entityType->getBundleEntityType()->willReturn($bundle_entity_type);
|
||||
$this->entityType->getKey('bundle')->willReturn($bundle_key);
|
||||
$link_template = 'add-form';
|
||||
$this->registerLinkTemplate($link_template);
|
||||
|
||||
/** @var \Drupal\Core\Url $url */
|
||||
$url = $entity->toUrl($link_template);
|
||||
$this->assertUrl('entity.test_entity.add_form', $expected_route_parameters, $entity, FALSE, $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides data for testToUrlLinkTemplateAddForm().
|
||||
*
|
||||
* @return array
|
||||
* An array of test cases for testToUrlLinkTemplateAddForm().
|
||||
*/
|
||||
public function providerTestToUrlLinkTemplateAddForm() {
|
||||
$test_cases = [];
|
||||
|
||||
$route_parameters = [];
|
||||
$test_cases['no_bundle_key'] = [static::HAS_NO_BUNDLE_KEY, NULL, FALSE, $route_parameters];
|
||||
|
||||
$route_parameters = ['type' => $this->entityTypeId];
|
||||
$test_cases['bundle_entity_type'] = [static::HAS_BUNDLE_KEY, 'type', FALSE, $route_parameters];
|
||||
$test_cases['bundle_key'] = [static::HAS_BUNDLE_KEY, NULL, 'type', $route_parameters];
|
||||
|
||||
return $test_cases;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Drupal\Tests\Core\ParamConverter;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\EntityRepositoryInterface;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\Core\ParamConverter\EntityRevisionParamConverter;
|
||||
|
@ -28,7 +29,10 @@ class EntityRevisionParamConverterTest extends UnitTestCase {
|
|||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->converter = new EntityRevisionParamConverter($this->prophesize(EntityTypeManagerInterface::class)->reveal());
|
||||
$this->converter = new EntityRevisionParamConverter(
|
||||
$this->prophesize(EntityTypeManagerInterface::class)->reveal(),
|
||||
$this->prophesize(EntityRepositoryInterface::class)->reveal()
|
||||
);
|
||||
}
|
||||
|
||||
protected function getTestRoute() {
|
||||
|
@ -67,7 +71,9 @@ class EntityRevisionParamConverterTest extends UnitTestCase {
|
|||
|
||||
$entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
|
||||
$entity_type_manager->getStorage('test')->willReturn($storage->reveal());
|
||||
$converter = new EntityRevisionParamConverter($entity_type_manager->reveal());
|
||||
$entity_repository = $this->prophesize(EntityRepositoryInterface::class);
|
||||
$entity_repository->getTranslationFromContext($entity)->willReturn($entity);
|
||||
$converter = new EntityRevisionParamConverter($entity_type_manager->reveal(), $entity_repository->reveal());
|
||||
|
||||
$route = $this->getTestRoute();
|
||||
$result = $converter->convert(1, $route->getOption('parameters')['test_revision'], 'test_revision', ['test_revision' => 1]);
|
||||
|
|
Reference in a new issue