You are here

public function UpcastingTest::testUpcasting in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/ParamConverter/UpcastingTest.php \Drupal\Tests\system\Functional\ParamConverter\UpcastingTest::testUpcasting()
  2. 10 core/modules/system/tests/src/Functional/ParamConverter/UpcastingTest.php \Drupal\Tests\system\Functional\ParamConverter\UpcastingTest::testUpcasting()

Confirms that all parameters are converted as expected.

All of these requests end up being processed by a controller with the signature: f($user, $node, $foo) returning either values or labels like "user: Dries, node: First post, foo: bar"

The test shuffles the parameters around and checks if the right thing is happening.

File

core/modules/system/tests/src/Functional/ParamConverter/UpcastingTest.php, line 32

Class

UpcastingTest
Tests upcasting of url arguments to entities.

Namespace

Drupal\Tests\system\Functional\ParamConverter

Code

public function testUpcasting() {
  $node = $this
    ->drupalCreateNode([
    'title' => $this
      ->randomMachineName(8),
  ]);
  $user = $this
    ->drupalCreateUser([
    'access content',
  ]);
  $foo = 'bar';

  // paramconverter_test/test_user_node_foo/{user}/{node}/{foo}
  $this
    ->drupalGet("paramconverter_test/test_user_node_foo/" . $user
    ->id() . '/' . $node
    ->id() . "/{$foo}");
  $this
    ->assertRaw("user: {$user->label()}, node: {$node->label()}, foo: {$foo}", 'user and node upcast by entity name');

  // paramconverter_test/test_node_user_user/{node}/{foo}/{user}
  // options.parameters.foo.type = entity:user
  $this
    ->drupalGet("paramconverter_test/test_node_user_user/" . $node
    ->id() . "/" . $user
    ->id() . "/" . $user
    ->id());
  $this
    ->assertRaw("user: {$user->label()}, node: {$node->label()}, foo: {$user->label()}", 'foo converted to user as well');

  // paramconverter_test/test_node_node_foo/{user}/{node}/{foo}
  // options.parameters.user.type = entity:node
  $this
    ->drupalGet("paramconverter_test/test_node_node_foo/" . $node
    ->id() . "/" . $node
    ->id() . "/{$foo}");
  $this
    ->assertRaw("user: {$node->label()}, node: {$node->label()}, foo: {$foo}", 'user is upcast to node (rather than to user)');
}