3 namespace Drupal\Tests\system\Functional\ParamConverter;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
9 * Tests upcasting of url arguments to entities.
11 * @group ParamConverter
13 class UpcastingTest extends BrowserTestBase {
15 public static $modules = ['paramconverter_test', 'node', 'language'];
18 * Confirms that all parameters are converted as expected.
20 * All of these requests end up being processed by a controller with the
21 * signature: f($user, $node, $foo) returning either values or labels
22 * like "user: Dries, node: First post, foo: bar"
24 * The tests shuffle the parameters around an checks if the right thing is
27 public function testUpcasting() {
28 $node = $this->drupalCreateNode(['title' => $this->randomMachineName(8)]);
29 $user = $this->drupalCreateUser(['access content']);
32 // paramconverter_test/test_user_node_foo/{user}/{node}/{foo}
33 $this->drupalGet("paramconverter_test/test_user_node_foo/" . $user->id() . '/' . $node->id() . "/$foo");
34 $this->assertRaw("user: {$user->label()}, node: {$node->label()}, foo: $foo", 'user and node upcast by entity name');
36 // paramconverter_test/test_node_user_user/{node}/{foo}/{user}
37 // options.parameters.foo.type = entity:user
38 $this->drupalGet("paramconverter_test/test_node_user_user/" . $node->id() . "/" . $user->id() . "/" . $user->id());
39 $this->assertRaw("user: {$user->label()}, node: {$node->label()}, foo: {$user->label()}", 'foo converted to user as well');
41 // paramconverter_test/test_node_node_foo/{user}/{node}/{foo}
42 // options.parameters.user.type = entity:node
43 $this->drupalGet("paramconverter_test/test_node_node_foo/" . $node->id() . "/" . $node->id() . "/$foo");
44 $this->assertRaw("user: {$node->label()}, node: {$node->label()}, foo: $foo", 'user is upcast to node (rather than to user)');
48 * Confirms we can upcast to controller arguments of the same type.
50 public function testSameTypes() {
51 $node = $this->drupalCreateNode(['title' => $this->randomMachineName(8)]);
52 $parent = $this->drupalCreateNode(['title' => $this->randomMachineName(8)]);
53 // paramconverter_test/node/{node}/set/parent/{parent}
54 // options.parameters.parent.type = entity:node
55 $this->drupalGet("paramconverter_test/node/" . $node->id() . "/set/parent/" . $parent->id());
56 $this->assertRaw("Setting '" . $parent->getTitle() . "' as parent of '" . $node->getTitle() . "'.");
60 * Confirms entity is shown in user's language by default.
62 public function testEntityLanguage() {
63 $language = ConfigurableLanguage::createFromLangcode('de');
65 \Drupal::configFactory()->getEditable('language.negotiation')
66 ->set('url.prefixes', ['de' => 'de'])
69 // The container must be recreated after adding a new language.
70 $this->rebuildContainer();
72 $node = $this->drupalCreateNode(['title' => 'English label']);
73 $translation = $node->addTranslation('de');
74 $translation->setTitle('Deutscher Titel')->save();
76 $this->drupalGet("/paramconverter_test/node/" . $node->id() . "/test_language");
77 $this->assertRaw("English label");
78 $this->drupalGet("paramconverter_test/node/" . $node->id() . "/test_language", ['language' => $language]);
79 $this->assertRaw("Deutscher Titel");