DynamicMenuLinkMock.php

Same filename in other branches
  1. 9 core/tests/Drupal/Tests/Core/Menu/DynamicMenuLinkMock.php
  2. 10 core/tests/Drupal/Tests/Core/Menu/DynamicMenuLinkMock.php
  3. 11.x core/tests/Drupal/Tests/Core/Menu/DynamicMenuLinkMock.php

Namespace

Drupal\Tests\Core\Menu

File

core/tests/Drupal/Tests/Core/Menu/DynamicMenuLinkMock.php

View source
<?php

namespace Drupal\Tests\Core\Menu;

use Drupal\Core\Session\AccountInterface;

/**
 * Defines a mock implementation of a dynamic menu link used in tests only.
 *
 * Has a dynamic route and title. This is rather contrived, but there are valid
 * use cases.
 *
 * @see \Drupal\user\Plugin\Menu\LoginLogoutMenuLink
 */
class DynamicMenuLinkMock extends MenuLinkMock {
    
    /**
     * The current user.
     *
     * @var \Drupal\Core\Session\AccountInterface
     */
    protected $currentUser;
    
    /**
     * Sets the current user.
     *
     * Allows the menu link to return the right title and route.
     *
     * @param \Drupal\Core\Session\AccountInterface $current_user
     *   The current user.
     *
     * @return $this
     */
    public function setCurrentUser(AccountInterface $current_user) {
        $this->currentUser = $current_user;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getTitle() {
        if ($this->currentUser
            ->isAuthenticated()) {
            return 'Log out';
        }
        else {
            return 'Log in';
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRouteName() {
        if ($this->currentUser
            ->isAuthenticated()) {
            return 'user.logout';
        }
        else {
            return 'user.login';
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheContexts() {
        return [
            'user.roles:authenticated',
        ];
    }

}

Classes

Title Deprecated Summary
DynamicMenuLinkMock Defines a mock implementation of a dynamic menu link used in tests only.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.