<?php

declare(strict_types=1);

namespace Drupal\Tests\simple_interactive_maps\Functional;

use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Verifies the "administer interactive_map" permission gates admin routes.
 */
#[Group('simple_interactive_maps')]
#[RunTestsInSeparateProcesses]
final class InteractiveMapPermissionsTest extends BrowserTestBase {

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

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'field_group',
    'simple_interactive_maps',
  ];

  /**
   * Tests that an anonymous user is denied.
   */
  public function testAnonymousUserIsDenied(): void {
    $this->drupalGet('/admin/structure/interactive-map');
    $this->assertSession()->statusCodeEquals(403);

    $this->drupalGet('/admin/structure/interactive_map/add');
    $this->assertSession()->statusCodeEquals(403);
  }

  /**
   * Tests that a privileged user is allowed.
   */
  public function testPrivilegedUserIsAllowed(): void {
    $account = $this->drupalCreateUser(['administer interactive_map']);
    $this->drupalLogin($account);

    $this->drupalGet('/admin/structure/interactive-map');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Interactive Map configuration');

    $this->drupalGet('/admin/structure/interactive_map/add');
    $this->assertSession()->statusCodeEquals(200);
  }

  /**
   * Tests that an unprivileged user is denied.
   */
  public function testUnprivilegedUserIsDenied(): void {
    $account = $this->drupalCreateUser(['access content']);
    $this->drupalLogin($account);

    $this->drupalGet('/admin/structure/interactive-map');
    $this->assertSession()->statusCodeEquals(403);
  }

}
