You are here

public function UserProfilePageTest::testUserProfilePage in Context 8.4

Tests the view inclusion condition against a view path/route.

File

tests/src/Kernel/UserProfilePageTest.php, line 71

Class

UserProfilePageTest
Tests the user profile page condition plugin.

Namespace

Drupal\Tests\context\Kernel

Code

public function testUserProfilePage() {
  $request = Request::create('');
  $request->attributes
    ->set('_route_object', new Route('/node/'));
  $this->requestStack
    ->push($request);

  /** @var \Drupal\context\Plugin\Condition\UserProfilePage $condition */
  $condition = $this->pluginManager
    ->createInstance('user_status');

  // Checks if viewing_profile is set to not work on nodes.
  $condition
    ->setConfig('user_status', [
    'viewing_profile' => 'viewing_profile',
  ]);
  $condition
    ->setConfig('user_fields', '');
  $this
    ->assertFalse($condition
    ->evaluate(), 'The user status field is not viewing_profile');

  // Checks if no condition are set to work on nodes.
  $condition
    ->setConfig('user_status', '');
  $this
    ->assertTrue($condition
    ->evaluate(), 'The user status field is empty');

  // Checks if viewing_profile is set to work on user/id page.
  $request = Request::create('/user/2');
  $request->attributes
    ->set('_route', 'entity.user.canonical');
  $request->request
    ->set('user', 2);
  $request->attributes
    ->set('_raw_variables', new ParameterBag([
    'user' => 2,
  ]));
  $request->attributes
    ->set('_route_object', new Route('/user/{user}'));
  $this->requestStack
    ->push($request);
  $condition
    ->setConfig('user_status', [
    'viewing_profile' => 'viewing_profile',
  ]);
  $this
    ->assertTrue($condition
    ->evaluate(), 'The user status field is viewing_profile');

  // Checks if own_page_true is set to work only on
  // currently logged in user/id page.
  $condition
    ->setConfig('user_status', [
    'own_page_true' => 'own_page_true',
  ]);
  $this
    ->assertTrue($condition
    ->evaluate(), 'The user status field is own_page_true');

  // Checks if logged_viewing_profile is set to work on any user/id page.
  $this
    ->setUpCurrentUser([
    'uid' => 3,
  ], [
    'access user profiles',
  ]);
  $condition
    ->setConfig('user_status', [
    'logged_viewing_profile' => 'logged_viewing_profile',
  ]);
  $this
    ->assertTrue($condition
    ->evaluate(), 'The user status field is logged_viewing_profile');

  // Checks if field_value is set to work if uid is present/not empty.
  $condition
    ->setConfig('user_status', [
    'field_value' => 'field_value',
  ]);
  $condition
    ->setConfig('user_fields', 'uid');
  $this
    ->assertTrue($condition
    ->evaluate(), 'The user status field is uid');

  // Checks if field_value is set to work if roles are present/not empty.
  $request = Request::create('/user/3/edit');
  $request->attributes
    ->set('_route', 'entity.user.edit_form');
  $request->request
    ->set('user', 3);
  $request->attributes
    ->set('_raw_variables', new ParameterBag([
    'user' => 3,
  ]));
  $request->attributes
    ->set('_route_object', new Route('/user/{user}/edit'));
  $this->requestStack
    ->push($request);
  $condition
    ->setConfig('user_fields', 'roles');
  $this
    ->assertTrue($condition
    ->evaluate(), 'The user status is field value and user field is roles');
}