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
|
@ -3,6 +3,7 @@
|
|||
namespace Drupal\FunctionalJavascriptTests;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
use Zumba\GastonJS\Exception\DeadClient;
|
||||
use Zumba\Mink\Driver\PhantomJSDriver;
|
||||
|
||||
/**
|
||||
|
@ -30,7 +31,16 @@ abstract class JavascriptTestBase extends BrowserTestBase {
|
|||
if (!file_exists($path)) {
|
||||
mkdir($path);
|
||||
}
|
||||
return parent::initMink();
|
||||
|
||||
try {
|
||||
return parent::initMink();
|
||||
}
|
||||
catch (DeadClient $e) {
|
||||
$this->markTestSkipped('PhantomJS is either not installed or not running. Start it via phantomjs --ssl-protocol=any --ignore-ssl-errors=true vendor/jcalderonzumba/gastonjs/src/Client/main.js 8510 1024 768&');
|
||||
}
|
||||
catch (Exception $e) {
|
||||
$this->markTestSkipped('An unexpected error occurred while starting Mink: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -102,6 +112,30 @@ abstract class JavascriptTestBase extends BrowserTestBase {
|
|||
$this->assertTrue($result, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a screenshot.
|
||||
*
|
||||
* @param string $filename
|
||||
* The file name of the resulting screenshot. If using the default phantomjs
|
||||
* driver then this should be a JPG filename.
|
||||
* @param bool $set_background_color
|
||||
* (optional) By default this method will set the background color to white.
|
||||
* Set to FALSE to override this behaviour.
|
||||
*
|
||||
* @throws \Behat\Mink\Exception\UnsupportedDriverActionException
|
||||
* When operation not supported by the driver.
|
||||
* @throws \Behat\Mink\Exception\DriverException
|
||||
* When the operation cannot be done.
|
||||
*/
|
||||
protected function createScreenshot($filename, $set_background_color = TRUE) {
|
||||
$session = $this->getSession();
|
||||
if ($set_background_color) {
|
||||
$session->executeScript("document.body.style.backgroundColor = 'white';");
|
||||
}
|
||||
$image = $session->getScreenshot();
|
||||
file_put_contents($filename, $image);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
@ -93,6 +93,58 @@ trait AssertLegacyTrait {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Passes if the text is found ONLY ONCE on the text version of the page.
|
||||
*
|
||||
* The text version is the equivalent of what a user would see when viewing
|
||||
* through a web browser. In other words the HTML has been filtered out of
|
||||
* the contents.
|
||||
*
|
||||
* @param string|\Drupal\Component\Render\MarkupInterface $text
|
||||
* Plain text to look for.
|
||||
* @param string $message
|
||||
* (optional) A message to display with the assertion. Do not translate
|
||||
* messages with t(). If left blank, a default message will be displayed.
|
||||
*
|
||||
* @deprecated Scheduled for removal in Drupal 9.0.0.
|
||||
* Use $this->getSession()->getPage()->getText() and substr_count() instead.
|
||||
*/
|
||||
protected function assertUniqueText($text, $message = NULL) {
|
||||
// Cast MarkupInterface objects to string.
|
||||
$text = (string) $text;
|
||||
|
||||
$message = $message ?: "'$text' found only once on the page";
|
||||
$page_text = $this->getSession()->getPage()->getText();
|
||||
$nr_found = substr_count($page_text, $text);
|
||||
$this->assertSame(1, $nr_found, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Passes if the text is found MORE THAN ONCE on the text version of the page.
|
||||
*
|
||||
* The text version is the equivalent of what a user would see when viewing
|
||||
* through a web browser. In other words the HTML has been filtered out of
|
||||
* the contents.
|
||||
*
|
||||
* @param string|\Drupal\Component\Render\MarkupInterface $text
|
||||
* Plain text to look for.
|
||||
* @param string $message
|
||||
* (optional) A message to display with the assertion. Do not translate
|
||||
* messages with t(). If left blank, a default message will be displayed.
|
||||
*
|
||||
* @deprecated Scheduled for removal in Drupal 9.0.0.
|
||||
* Use $this->getSession()->getPage()->getText() and substr_count() instead.
|
||||
*/
|
||||
protected function assertNoUniqueText($text, $message = '') {
|
||||
// Cast MarkupInterface objects to string.
|
||||
$text = (string) $text;
|
||||
|
||||
$message = $message ?: "'$text' found more than once on the page";
|
||||
$page_text = $this->getSession()->getPage()->getText();
|
||||
$nr_found = substr_count($page_text, $text);
|
||||
$this->assertGreaterThan(1, $nr_found, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the page responds with the specified response code.
|
||||
*
|
||||
|
@ -128,6 +180,27 @@ trait AssertLegacyTrait {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a field exists with the given name and value.
|
||||
*
|
||||
* @param string $name
|
||||
* Name of field to assert.
|
||||
* @param string $value
|
||||
* (optional) Value of the field to assert. You may pass in NULL (default)
|
||||
* to skip checking the actual value, while still checking that the field
|
||||
* exists.
|
||||
*
|
||||
* @deprecated Scheduled for removal in Drupal 9.0.0.
|
||||
* Use $this->assertSession()->fieldNotExists() or
|
||||
* $this->assertSession()->fieldValueNotEquals() instead.
|
||||
*/
|
||||
protected function assertNoFieldByName($name, $value = NULL) {
|
||||
$this->assertSession()->fieldNotExists($name);
|
||||
if ($value !== NULL) {
|
||||
$this->assertSession()->fieldValueNotEquals($name, (string) $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a field exists with the given ID and value.
|
||||
*
|
||||
|
@ -343,6 +416,53 @@ trait AssertLegacyTrait {
|
|||
return $this->assertSession()->optionNotExists($id, $option);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a select option in the current page is checked.
|
||||
*
|
||||
* @param string $id
|
||||
* ID of select field to assert.
|
||||
* @param string $option
|
||||
* Option to assert.
|
||||
* @param string $message
|
||||
* (optional) A message to display with the assertion. Do not translate
|
||||
* messages with t(). If left blank, a default message will be displayed.
|
||||
*
|
||||
* @deprecated Scheduled for removal in Drupal 9.0.0.
|
||||
* Use $this->assertSession()->optionExists() instead and check the
|
||||
* "selected" attribute yourself.
|
||||
*/
|
||||
protected function assertOptionSelected($id, $option, $message = NULL) {
|
||||
$option_field = $this->assertSession()->optionExists($id, $option);
|
||||
$message = $message ?: "Option $option for field $id is selected.";
|
||||
$this->assertTrue($option_field->hasAttribute('selected'), $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a checkbox field in the current page is checked.
|
||||
*
|
||||
* @param string $id
|
||||
* ID of field to assert.
|
||||
*
|
||||
* @deprecated Scheduled for removal in Drupal 9.0.0.
|
||||
* Use $this->assertSession()->checkboxChecked() instead.
|
||||
*/
|
||||
protected function assertFieldChecked($id) {
|
||||
$this->assertSession()->checkboxChecked($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a checkbox field in the current page is not checked.
|
||||
*
|
||||
* @param string $id
|
||||
* ID of field to assert.
|
||||
*
|
||||
* @deprecated Scheduled for removal in Drupal 9.0.0.
|
||||
* Use $this->assertSession()->checkboxNotChecked() instead.
|
||||
*/
|
||||
protected function assertNoFieldChecked($id) {
|
||||
$this->assertSession()->checkboxNotChecked($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Passes if the raw text IS found escaped on the loaded page, fail otherwise.
|
||||
*
|
||||
|
@ -373,6 +493,19 @@ trait AssertLegacyTrait {
|
|||
$this->assertSession()->assertNoEscaped($raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers a pass if the Perl regex pattern is found in the raw content.
|
||||
*
|
||||
* @param string $pattern
|
||||
* Perl regex to look for including the regex delimiters.
|
||||
*
|
||||
* @deprecated Scheduled for removal in Drupal 9.0.0.
|
||||
* Use $this->assertSession()->responseMatches() instead.
|
||||
*/
|
||||
protected function assertPattern($pattern) {
|
||||
$this->assertSession()->responseMatches($pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts whether an expected cache tag was present in the last response.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Database;
|
||||
|
||||
use Drupal\Core\Database\Database;
|
||||
|
||||
/**
|
||||
* Tests that the prefix info for a database schema is correct.
|
||||
*
|
||||
* @group Database
|
||||
*/
|
||||
class PrefixInfoTest extends DatabaseTestBase {
|
||||
|
||||
/**
|
||||
* Tests that DatabaseSchema::getPrefixInfo() returns the right database.
|
||||
*
|
||||
* We are testing if the return array of the method
|
||||
* \Drupal\Core\Database\Driver\mysql\Schema::getPrefixInfo(). This return
|
||||
* array is a keyed array with info about amongst other things the database.
|
||||
* The other two by Drupal core supported databases do not have this variable
|
||||
* set in the return array.
|
||||
*/
|
||||
function testGetPrefixInfo() {
|
||||
$connection_info = Database::getConnectionInfo('default');
|
||||
if ($connection_info['default']['driver'] == 'mysql') {
|
||||
// Copy the default connection info to the 'extra' key.
|
||||
Database::addConnectionInfo('extra', 'default', $connection_info['default']);
|
||||
|
||||
$db1_connection = Database::getConnection('default', 'default');
|
||||
$db1_schema = $db1_connection->schema();
|
||||
$db2_connection = Database::getConnection('default', 'extra');
|
||||
|
||||
// Get the prefix info for the first databse.
|
||||
$method = new \ReflectionMethod($db1_schema, 'getPrefixInfo');
|
||||
$method->setAccessible(TRUE);
|
||||
$db1_info = $method->invoke($db1_schema);
|
||||
|
||||
// We change the database after opening the connection, so as to prevent
|
||||
// connecting to a non-existent database.
|
||||
$reflection = new \ReflectionObject($db2_connection);
|
||||
$property = $reflection->getProperty('connectionOptions');
|
||||
$property->setAccessible(TRUE);
|
||||
$connection_info['default']['database'] = 'foobar';
|
||||
$property->setValue($db2_connection, $connection_info['default']);
|
||||
|
||||
// For testing purposes, we also change the database info.
|
||||
$reflection_class = new \ReflectionClass('Drupal\Core\Database\Database');
|
||||
$property = $reflection_class->getProperty('databaseInfo');
|
||||
$property->setAccessible(TRUE);
|
||||
$info = $property->getValue();
|
||||
$info['extra']['default']['database'] = 'foobar';
|
||||
$property->setValue(NULL, $info);
|
||||
|
||||
$extra_info = Database::getConnectionInfo('extra');
|
||||
$this->assertSame($extra_info['default']['database'], 'foobar');
|
||||
$db2_schema = $db2_connection->schema();
|
||||
$db2_info = $method->invoke($db2_schema);
|
||||
|
||||
$this->assertNotSame($db2_info['database'], $db1_info['database'], 'Each target connection has a different database.');
|
||||
$this->assertSame($db2_info['database'], 'foobar', 'The new profile has a different database.');
|
||||
|
||||
Database::removeConnection('extra');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -154,6 +154,20 @@ class SelectComplexTest extends DatabaseTestBase {
|
|||
$this->assertEqual($query_result, 2, 'Returned the correct number of rows.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether the range property of a select clause can be undone.
|
||||
*/
|
||||
function testRangeUndo() {
|
||||
$query = db_select('test');
|
||||
$name_field = $query->addField('test', 'name');
|
||||
$age_field = $query->addField('test', 'age', 'age');
|
||||
$query->range(0, 2);
|
||||
$query->range(NULL, NULL);
|
||||
$query_result = $query->countQuery()->execute()->fetchField();
|
||||
|
||||
$this->assertEqual($query_result, 4, 'Returned the correct number of rows.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests distinct queries.
|
||||
*/
|
||||
|
|
|
@ -322,6 +322,64 @@ class SelectTest extends DatabaseTestBase {
|
|||
$this->assertEqual(count($names), $count, "The count query's result matched the number of rows in the UNION query.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that we can UNION multiple Select queries together and set the ORDER.
|
||||
*/
|
||||
function testUnionOrder() {
|
||||
// This gives George and Ringo.
|
||||
$query_1 = db_select('test', 't')
|
||||
->fields('t', array('name'))
|
||||
->condition('age', array(27, 28), 'IN');
|
||||
|
||||
// This gives Paul.
|
||||
$query_2 = db_select('test', 't')
|
||||
->fields('t', array('name'))
|
||||
->condition('age', 26);
|
||||
|
||||
$query_1->union($query_2);
|
||||
$query_1->orderBy('name', 'DESC');
|
||||
|
||||
$names = $query_1->execute()->fetchCol();
|
||||
|
||||
// Ensure we get all 3 records.
|
||||
$this->assertEqual(count($names), 3, 'UNION returned rows from both queries.');
|
||||
|
||||
// Ensure that the names are in the correct reverse alphabetical order,
|
||||
// regardless of which query they came from.
|
||||
$this->assertEqual($names[0], 'Ringo', 'First query returned correct name.');
|
||||
$this->assertEqual($names[1], 'Paul', 'Second query returned correct name.');
|
||||
$this->assertEqual($names[2], 'George', 'Third query returned correct name.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that we can UNION multiple Select queries together with and a LIMIT.
|
||||
*/
|
||||
function testUnionOrderLimit() {
|
||||
// This gives George and Ringo.
|
||||
$query_1 = db_select('test', 't')
|
||||
->fields('t', array('name'))
|
||||
->condition('age', array(27, 28), 'IN');
|
||||
|
||||
// This gives Paul.
|
||||
$query_2 = db_select('test', 't')
|
||||
->fields('t', array('name'))
|
||||
->condition('age', 26);
|
||||
|
||||
$query_1->union($query_2);
|
||||
$query_1->orderBy('name', 'DESC');
|
||||
$query_1->range(0, 2);
|
||||
|
||||
$names = $query_1->execute()->fetchCol();
|
||||
|
||||
// Ensure we get all only 2 of the 3 records.
|
||||
$this->assertEqual(count($names), 2, 'UNION with a limit returned rows from both queries.');
|
||||
|
||||
// Ensure that the names are in the correct reverse alphabetical order,
|
||||
// regardless of which query they came from.
|
||||
$this->assertEqual($names[0], 'Ringo', 'First query returned correct name.');
|
||||
$this->assertEqual($names[1], 'Paul', 'Second query returned correct name.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that random ordering of queries works.
|
||||
*
|
||||
|
|
|
@ -62,4 +62,31 @@ class ContentEntityCloneTest extends EntityKernelTestBase {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the flag for enforcing a new entity is not shared.
|
||||
*/
|
||||
public function testEnforceIsNewOnClonedEntityTranslation() {
|
||||
// Create a test entity.
|
||||
$entity = EntityTestMul::create([
|
||||
'name' => $this->randomString(),
|
||||
'language' => 'en',
|
||||
]);
|
||||
$entity->save();
|
||||
$entity_translation = $entity->addTranslation('de');
|
||||
$entity->save();
|
||||
|
||||
// The entity is not new anymore.
|
||||
$this->assertFalse($entity_translation->isNew());
|
||||
|
||||
// The clone should not be new as well.
|
||||
$clone = clone $entity_translation;
|
||||
$this->assertFalse($clone->isNew());
|
||||
|
||||
// After enforcing the clone to be new only it should be flagged as new,
|
||||
// but the original entity should not be flagged as new.
|
||||
$clone->enforceIsNew();
|
||||
$this->assertTrue($clone->isNew());
|
||||
$this->assertFalse($entity_translation->isNew());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -174,4 +174,37 @@ class ContentEntityNonRevisionableFieldTest extends EntityKernelTestBase {
|
|||
$this->assertEquals($expected_non_rev_field_revision_ids, $non_rev_field_revision_ids, 'Revision ids found');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests multi column non revisionable base field for revisionable entity.
|
||||
*/
|
||||
public function testMultiColumnNonRevisionableBaseField() {
|
||||
\Drupal::state()->set('entity_test.multi_column', TRUE);
|
||||
\Drupal::entityDefinitionUpdateManager()->applyUpdates();
|
||||
// Refresh the storage.
|
||||
$this->mulRev = $this->entityManager->getStorage('entity_test_mulrev');
|
||||
$user1 = $this->createUser();
|
||||
|
||||
// Create a test entity.
|
||||
$entity = EntityTestMulRev::create([
|
||||
'name' => $this->randomString(),
|
||||
'user_id' => $user1->id(),
|
||||
'language' => 'en',
|
||||
'non_rev_field' => 'Huron',
|
||||
'description' => [
|
||||
'shape' => 'shape',
|
||||
'color' => 'color',
|
||||
],
|
||||
]);
|
||||
$entity->save();
|
||||
$entity = $this->mulRev->loadUnchanged($entity->id());
|
||||
$expected = [
|
||||
[
|
||||
'shape' => 'shape',
|
||||
'color' => 'color',
|
||||
],
|
||||
];
|
||||
$this->assertEquals('Huron', $entity->get('non_rev_field')->value, 'Huron found on entity 1');
|
||||
$this->assertEquals($expected, $entity->description->getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -87,4 +87,49 @@ class EntityRevisionTranslationTest extends EntityKernelTestBase {
|
|||
$this->assertFalse($entity->hasTranslation('de'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the translation values when saving a forward revision.
|
||||
*/
|
||||
public function testTranslationValuesWhenSavingForwardRevisions() {
|
||||
$user = $this->createUser();
|
||||
$storage = $this->entityManager->getStorage('entity_test_mulrev');
|
||||
|
||||
// Create a test entity and a translation for it.
|
||||
$entity = EntityTestMulRev::create([
|
||||
'name' => 'default revision - en',
|
||||
'user_id' => $user->id(),
|
||||
'language' => 'en',
|
||||
]);
|
||||
$entity->addTranslation('de', ['name' => 'default revision - de']);
|
||||
$entity->save();
|
||||
|
||||
// Create a forward revision for the entity and change a field value for
|
||||
// both languages.
|
||||
$forward_revision = $this->reloadEntity($entity);
|
||||
|
||||
$forward_revision->setNewRevision();
|
||||
$forward_revision->isDefaultRevision(FALSE);
|
||||
|
||||
$forward_revision->name = 'forward revision - en';
|
||||
$forward_revision->save();
|
||||
|
||||
$forward_revision_translation = $forward_revision->getTranslation('de');
|
||||
$forward_revision_translation->name = 'forward revision - de';
|
||||
$forward_revision_translation->save();
|
||||
|
||||
$forward_revision_id = $forward_revision->getRevisionId();
|
||||
$forward_revision = $storage->loadRevision($forward_revision_id);
|
||||
|
||||
// Change the value of the field in the default language, save the forward
|
||||
// revision and check that the value of the field in the second language is
|
||||
// also taken from the forward revision, *not* from the default revision.
|
||||
$forward_revision->name = 'updated forward revision - en';
|
||||
$forward_revision->save();
|
||||
|
||||
$forward_revision = $storage->loadRevision($forward_revision_id);
|
||||
|
||||
$this->assertEquals($forward_revision->name->value, 'updated forward revision - en');
|
||||
$this->assertEquals($forward_revision->getTranslation('de')->name->value, 'forward revision - de');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -165,6 +165,7 @@ class EntityViewBuilderTest extends EntityKernelTestBase {
|
|||
$this->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == ['tags', 'contexts', 'max-age'], 'A view mode with render cache disabled has the correct output (only cache tags, contexts and max-age).');
|
||||
|
||||
// Test that an entity type can opt out of render caching completely.
|
||||
$this->installEntitySchema('entity_test_label');
|
||||
$entity_test_no_cache = $this->createTestEntity('entity_test_label');
|
||||
$entity_test_no_cache->save();
|
||||
$build = $this->container->get('entity.manager')->getViewBuilder('entity_test_label')->view($entity_test_no_cache, 'full');
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Entity;
|
||||
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
|
||||
/**
|
||||
* Test view/render hooks for entities.
|
||||
*
|
||||
* @todo Add tests for the following hooks. https://www.drupal.org/node/2755353
|
||||
* hook_entity_view_display_alter()
|
||||
* hook_entity_prepare_view()
|
||||
* hook_ENTITY_TYPE_view()
|
||||
* hook_entity_view()
|
||||
* hook_ENTITY_TYPE_view_alter()
|
||||
* hook_entity_view_alter()
|
||||
*
|
||||
* @group Entity
|
||||
*/
|
||||
class EntityViewHookTest extends EntityKernelTestBase {
|
||||
|
||||
/**
|
||||
* Test hook_entity_display_build_alter().
|
||||
*/
|
||||
public function testHookEntityDisplayBuildAlter() {
|
||||
entity_test_create_bundle('display_build_alter_bundle');
|
||||
/** @var \Drupal\Core\Render\RendererInterface $renderer */
|
||||
$renderer = $this->container->get('renderer');
|
||||
|
||||
$entity_ids = [];
|
||||
// Create some entities to test.
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
$entity = EntityTest::create([
|
||||
'name' => $this->randomMachineName(),
|
||||
'type' => 'display_build_alter_bundle',
|
||||
]);
|
||||
$entity->save();
|
||||
$entity_ids[] = $entity->id();
|
||||
}
|
||||
|
||||
/** @var \Drupal\entity_test\EntityTestViewBuilder $view_builder */
|
||||
$view_builder = $this->container->get('entity_type.manager')->getViewBuilder('entity_test');
|
||||
|
||||
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
|
||||
$storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
|
||||
$storage->resetCache();
|
||||
$entities = $storage->loadMultiple($entity_ids);
|
||||
|
||||
$build = $view_builder->viewMultiple($entities);
|
||||
|
||||
$output = $renderer->renderRoot($build);
|
||||
$this->setRawContent($output->__toString());
|
||||
// Confirm that the content added in
|
||||
// entity_test_entity_display_build_alter() appears multiple times, not
|
||||
// just for the final entity.
|
||||
foreach ($entity_ids as $id) {
|
||||
$this->assertText('Content added in hook_entity_display_build_alter for entity id ' . $id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -274,6 +274,7 @@ class FieldSqlStorageTest extends EntityKernelTestBase {
|
|||
function testLongNames() {
|
||||
// Use one of the longest entity_type names in core.
|
||||
$entity_type = $bundle = 'entity_test_label_callback';
|
||||
$this->installEntitySchema('entity_test_label_callback');
|
||||
$storage = $this->container->get('entity.manager')->getStorage($entity_type);
|
||||
|
||||
// Create two fields and generate random values.
|
||||
|
|
|
@ -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