Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / system / tests / src / Functional / ParamConverter / UpcastingTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\ParamConverter;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
7
8 /**
9  * Tests upcasting of url arguments to entities.
10  *
11  * @group ParamConverter
12  */
13 class UpcastingTest extends BrowserTestBase {
14
15   public static $modules = ['paramconverter_test', 'node', 'language'];
16
17   /**
18    * Confirms that all parameters are converted as expected.
19    *
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"
23    *
24    * The tests shuffle the parameters around an checks if the right thing is
25    * happening.
26    */
27   public function testUpcasting() {
28     $node = $this->drupalCreateNode(['title' => $this->randomMachineName(8)]);
29     $user = $this->drupalCreateUser(['access content']);
30     $foo = 'bar';
31
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');
35
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');
40
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)');
45   }
46
47   /**
48    * Confirms we can upcast to controller arguments of the same type.
49    */
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() . "'.");
57   }
58
59   /**
60    * Confirms entity is shown in user's language by default.
61    */
62   public function testEntityLanguage() {
63     $language = ConfigurableLanguage::createFromLangcode('de');
64     $language->save();
65     \Drupal::configFactory()->getEditable('language.negotiation')
66       ->set('url.prefixes', ['de' => 'de'])
67       ->save();
68
69     // The container must be recreated after adding a new language.
70     $this->rebuildContainer();
71
72     $node = $this->drupalCreateNode(['title' => 'English label']);
73     $translation = $node->addTranslation('de');
74     $translation->setTitle('Deutscher Titel')->save();
75
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");
80   }
81
82 }