Unverified Commit e6c69fa8 authored by Alex Pott's avatar Alex Pott
Browse files

task: #2699565 Refactor Basic Auth kernel tests to use dependency injection

By: heykarthikwithu
By: alexpott
By: alvar0hurtad0
By: smustgrave
By: sivaji_ganesh_jojodae
By: Swetha Yarla
parent 7b5d5ac9
Loading
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -4,16 +4,25 @@

namespace Drupal\basic_auth_test;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\PageCache\ResponsePolicyInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/**
 * Provides routes for HTTP Basic Authentication testing.
 */
class BasicAuthTestController {
class BasicAuthTestController extends ControllerBase {

  public function __construct(
    #[Autowire(service: 'page_cache_kill_switch')]
    private ResponsePolicyInterface $pageCacheKillSwitch,
  ) {}

  /**
   * @see \Drupal\basic_auth\Tests\Authentication\BasicAuthTest::testControllerNotCalledBeforeAuth()
   */
  public function modifyState() {
    \Drupal::state()->set('basic_auth_test.state.controller_executed', TRUE);
    $this->state()->set('basic_auth_test.state.controller_executed', TRUE);
    return ['#markup' => 'Done'];
  }

@@ -22,10 +31,10 @@ public function modifyState() {
   */
  public function readState() {
    // Mark this page as being uncacheable.
    \Drupal::service('page_cache_kill_switch')->trigger();
    $this->pageCacheKillSwitch->trigger();

    return [
      '#markup' => \Drupal::state()->get('basic_auth_test.state.controller_executed') ? 'yep' : 'nope',
      '#markup' => $this->state()->get('basic_auth_test.state.controller_executed') ? 'yep' : 'nope',
      '#cache' => [
        'max-age' => 0,
      ],
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ public function testBasicAuth(): void {
    // Ensure that the user is prompted to authenticate if they are not yet
    // authenticated and the route only allows basic auth.
    $this->drupalGet($url);
    $this->assertSession()->responseHeaderEquals('WWW-Authenticate', 'Basic realm="' . \Drupal::config('system.site')->get('name') . '"');
    $this->assertSession()->responseHeaderEquals('WWW-Authenticate', 'Basic realm="' . $this->config('system.site')->get('name') . '"');
    $this->assertSession()->statusCodeEquals(401);

    // Ensure that a route without basic auth defined doesn't prompt for auth.