Commit 42ce095c authored by Joshua Sedler's avatar Joshua Sedler 🤸🏼
Browse files

Issue #3275223: Write tests for typical use-cases

parent 5b768294
Loading
Loading
Loading
Loading

composer.json

deleted100644 → 0
+0 −8
Original line number Diff line number Diff line
{
  "name": "drupal/current_user_paths",
  "type": "drupal-module",
  "description": "Current user paths",
  "homepage": "https://www.drupal.org/project/current_user_paths",
  "license": "GPL-2.0+",
  "minimum-stability": "dev"
}
 No newline at end of file
+3 −15
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@ namespace Drupal\user_current_paths\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Path\PathValidator;

/**
@@ -13,13 +12,6 @@ use Drupal\Core\Path\PathValidator;
 */
class UserCurrentPathsController extends ControllerBase {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxy
   */
  protected $currentUser;

  /**
   * The path validator.
   *
@@ -30,13 +22,10 @@ class UserCurrentPathsController extends ControllerBase {
  /**
   * Constructs a UserCurrentPathsController object.
   *
   * @param \Drupal\Core\Session\AccountProxy $current_user
   *   The current user.
   * @param \Drupal\Core\Path\PathValidator $path_validator
   *   The path validator.
   */
  public function __construct(AccountProxy $current_user, PathValidator $path_validator) {
    $this->currentUser = $current_user;
  public function __construct(PathValidator $path_validator) {
    $this->pathValidator = $path_validator;
  }

@@ -45,7 +34,6 @@ class UserCurrentPathsController extends ControllerBase {
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('current_user'),
      $container->get('path.validator')
    );
  }
@@ -63,7 +51,7 @@ class UserCurrentPathsController extends ControllerBase {
   *   The wildcart action.
   */
  public function wildcardActionRedirect(string $wildcardaction) {
    $path = '/user/' . $this->currentUser->id();
    $path = '/user/' . $this->currentUser()->id();
    if ($wildcardaction != 'view') {
      // /view doesn't exist for user entities
      $path .= '/' . $wildcardaction;
@@ -84,7 +72,7 @@ class UserCurrentPathsController extends ControllerBase {
  public function editRedirect() {
    $route_name = 'entity.user.edit_form';
    $route_parameters = [
      'user' => $this->currentUser->id(),
      'user' => $this->currentUser()->id(),
    ];

    return $this->redirect($route_name, $route_parameters);
+129 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\user_current_paths\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * This class provides methods specifically for testing something.
 *
 * @group user_current_paths
 */
class UserCurrentPathFunctionalTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'node',
    'test_page_test',
    'user',
    'shortcut',
    'user_current_paths',
  ];

  /**
   * A user with authenticated permissions.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $user;

  /**
   * A user with admin permissions.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $adminUser;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->config('system.site')->set('page.front', '/test-page')->save();
    $this->user = $this->drupalCreateUser([], 'userNotLoggedIn', FALSE, ['id' => 2]);
    $this->adminUser = $this->drupalCreateUser([], 'userLoggedIn', TRUE, ['id' => 3]);
    $this->adminUser->addRole($this->createAdminRole('admin', 'admin'));
    $this->adminUser->save();
    $this->drupalLogin($this->adminUser);
  }

  /**
   * Tests if the module installation, won't break the site.
   */
  public function testInstallation() {
    $session = $this->assertSession();
    $this->drupalGet('<front>');
    $session->statusCodeEquals(200);
  }

  /**
   * Tests if uninstalling the module, won't break the site.
   */
  public function testUninstallation() {
    // Go to uninstallation page an uninstall user_current_paths:
    $session = $this->assertSession();
    $page = $this->getSession()->getPage();
    $this->drupalGet('/admin/modules/uninstall');
    $session->statusCodeEquals(200);
    $page->checkField('edit-uninstall-user-current-paths');
    $page->pressButton('edit-submit');
    $session->statusCodeEquals(200);
    // Confirm deinstall:
    $page->pressButton('edit-submit');
    $session->statusCodeEquals(200);
    $session->pageTextContains('The selected modules have been uninstalled.');
  }

  /**
   * Tests, that the /user/edit route redirects to the currently logged in user.
   */
  public function testUserEditRouteIsCurrentUser() {
    $session = $this->assertSession();
    $page = $this->getSession()->getPage();
    // Test first route:
    $this->drupalGet('/user/edit');
    $session->statusCodeEquals(200);
    $currentUrl = $this->getUrl();
    // Check if this page edits the current logged in user:
    $this->assertStringContainsString('user/3', $currentUrl);
    $session->elementAttributeContains('css', '#edit-name', 'value', 'userLoggedIn');
  }

  /**
   * Tests, that the /user/current/edit route redirects to the current user.
   */
  public function testUserCurrentRouteIsCurrentUser() {
    $session = $this->assertSession();
    $page = $this->getSession()->getPage();
    // Test first route:
    $this->drupalGet('/user/current/edit');
    $session->statusCodeEquals(200);
    $currentUrl = $this->getUrl();
    // Check if this page edits the current logged in user:
    $this->assertStringContainsString('user/3', $currentUrl);
    $session->elementAttributeContains('css', '#edit-name', 'value', 'userLoggedIn');
  }

  /**
   * Tests, user wildcard action.
   */
  public function testWildCardActions() {
    $session = $this->assertSession();
    $page = $this->getSession()->getPage();
    // Test user shortcut page:
    $this->drupalGet('/user/current/shortcuts');
    $session->statusCodeEquals(200);
    $currentUrl = $this->getUrl();
    $this->assertStringContainsString('user/3', $currentUrl);
    $session->pageTextContains('Shortcuts');
  }

}