Skip to content
Snippets Groups Projects

Issue #3500006 Add basic PHPUnit tests

Compare and
7 files
+ 781
47
Compare changes
  • Side-by-side
  • Inline

Files

 
<?php
 
 
declare(strict_types=1);
 
 
namespace Drupal\Tests\environment_indicator\Functional;
 
 
use Drupal\Core\StringTranslation\StringTranslationTrait;
 
use Drupal\Tests\BrowserTestBase;
 
 
/**
 
* Tests for Environment Indicator.
 
*
 
* @group environment_indicator
 
*/
 
class EnvironmentIndicatorTest extends BrowserTestBase {
 
 
use StringTranslationTrait;
 
 
/**
 
* {@inheritdoc}
 
*/
 
protected static $modules = [
 
'environment_indicator',
 
'environment_indicator_ui',
 
];
 
 
/**
 
* {@inheritdoc}
 
*/
 
protected $defaultTheme = 'stark';
 
 
/**
 
* A user with permission to see the environment indicator.
 
*
 
* @var \Drupal\user\UserInterface
 
*/
 
protected $privilegedUser;
 
 
/**
 
* A user without permission to see the environment indicator.
 
*
 
* @var \Drupal\user\UserInterface
 
*/
 
protected $unprivilegedUser;
 
 
/**
 
* A user with permission to create environment switchers.
 
*
 
* @var \Drupal\user\UserInterface
 
*/
 
protected $environmentIndicatorAdministrator;
 
 
/**
 
* The path to the environment_indicator module.
 
*
 
* @var string
 
*/
 
protected string $modulePath;
 
 
/**
 
* The state service.
 
*
 
* @var \Drupal\Core\State\StateInterface
 
*/
 
protected $state;
 
 
/**
 
* {@inheritdoc}
 
*/
 
protected function setUp(): void {
 
parent::setUp();
 
 
// Retrieve the dynamic module path.
 
$moduleHandler = \Drupal::service('extension.list.module');
 
$this->modulePath = $moduleHandler->getPath('environment_indicator');
 
 
$this->state = \Drupal::state();
 
 
// Create users.
 
$this->privilegedUser = $this->drupalCreateUser(['access environment indicator']);
 
$this->unprivilegedUser = $this->drupalCreateUser();
 
}
 
 
/**
 
* Tests that the environment indicator appears in the page top region.
 
*
 
* This test verifies that the environment indicator appears in the page top
 
* region with the expected attributes.
 
*/
 
public function testVisibilityWithPermissions(): void {
 
$config = $this->config('environment_indicator.indicator');
 
$config->set('name', 'Red Green Environment')
 
->set('fg_color', 'green')
 
->set('bg_color', 'red')
 
->save();
 
$this->container->get('cache_tags.invalidator')->invalidateTags(['config:environment_indicator.indicator']);
 
$this->drupalLogin($this->privilegedUser);
 
$this->drupalGet('<front>');
 
$session = $this->assertSession();
 
$session->pageTextContains('Red Green Environment');
 
$session->elementExists('css', '#environment-indicator');
 
$output = $this->getSession()->getPage()->find('css', '#environment-indicator')->getAttribute('style');
 
$this->assertNotEmpty($output, 'Style attribute should not be empty.');
 
$this->assertStringContainsString('background-color: red', $output);
 
$this->assertStringContainsString('color: green', $output);
 
$this->assertSession()->elementExists("css", "link[href*='{$this->modulePath}/css/environment_indicator.css']");
 
// Change configuration values.
 
$config = $this->config('environment_indicator.indicator');
 
$config->set('name', 'Test Environment')
 
->set('fg_color', '#ffffff')
 
->set('bg_color', '#000000')
 
->save();
 
// Clear drupal cache.
 
$this->container->get('cache_tags.invalidator')->invalidateTags(['config:environment_indicator.indicator']);
 
$this->drupalLogin($this->privilegedUser);
 
$this->drupalGet('<front>');
 
$session = $this->assertSession();
 
$session->pageTextContains('Test Environment');
 
$session->elementExists('css', '#environment-indicator');
 
$output = $this->getSession()->getPage()->find('css', '#environment-indicator')->getAttribute('style');
 
$this->assertNotEmpty($output, 'Style attribute should not be empty.');
 
$this->assertStringContainsString('background-color: #000000', $output);
 
$this->assertStringContainsString('color: #ffffff', $output);
 
 
// Change configuration values.
 
$config = $this->config('environment_indicator.indicator');
 
$config->set('name', 'Development Environment')
 
->set('fg_color', '#efefef')
 
->set('bg_color', '#12285f')
 
->save();
 
// Clear drupal cache.
 
$this->container->get('cache_tags.invalidator')->invalidateTags(['config:environment_indicator.indicator']);
 
$this->drupalGet('<front>');
 
// Assert that the environment indicator exists with
 
// the expected attributes.
 
$session->elementExists('css', '#environment-indicator');
 
$output = $this->getSession()->getPage()->find('css', '#environment-indicator')->getAttribute('style');
 
$this->assertNotEmpty($output, 'Style attribute should not be empty.');
 
$this->assertStringContainsString('background-color: #12285f', $output);
 
$this->assertStringContainsString('color: #efefef', $output);
 
$session->pageTextContains('Development Environment');
 
}
 
 
/**
 
* Tests visibility for unauthorized users.
 
*/
 
public function testVisibilityWithoutPermissions(): void {
 
$config = $this->config('environment_indicator.indicator');
 
$config->set('name', 'Test Environment')
 
->set('fg_color', '#000000')
 
->set('bg_color', '#ffffff')
 
->save();
 
$this->container->get('cache_tags.invalidator')->invalidateTags(['config:environment_indicator.indicator']);
 
$this->drupalLogin($this->drupalCreateUser());
 
$this->drupalGet('<front>');
 
$this->assertSession()->elementNotExists('css', '#environment-indicator');
 
$this->assertSession()->elementNotExists("css", "link[href*='{$this->modulePath}/css/environment_indicator.css']");
 
}
 
 
/**
 
* Tests the indicator with the default configuration.
 
*/
 
public function testIndicatorDefaultConfiguration(): void {
 
$this->drupalLogin($this->privilegedUser);
 
$this->state->set('environment_indicator.current_release', 'v1.2.44');
 
$config = $this->config('environment_indicator.indicator');
 
$config->set('name', 'Indicator Environment')
 
->set('fg_color', '#000000')
 
->set('bg_color', '#ffffff')
 
->save();
 
$this->container->get('cache_tags.invalidator')->invalidateTags(['config:environment_indicator.indicator']);
 
$this->drupalGet('<front>');
 
$output = $this->getSession()->getPage()->find('css', '#environment-indicator')->getAttribute('style');
 
$this->assertNotEmpty($output, 'Style attribute should not be empty.');
 
$this->assertStringContainsString('background-color: #ffffff', $output);
 
$this->assertStringContainsString('color: #000000', $output);
 
$this->assertSession()->elementExists('css', 'div#environment-indicator span.description');
 
$this->assertSession()->pageTextContains('v1.2.44');
 
}
 
 
}
Loading