RedirectManagerTest.php in Domain 301 Redirect 8        
                          
                  
                        
  
  
  
  
  
File
  tests/src/Functional/RedirectManagerTest.php
  
    View source  
  <?php
namespace Drupal\Tests\domain_301_redirect\Functional;
use Drupal;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
use Drupal\Tests\domain_301_redirect\Traits\Domain301RedirectFunctionalTestTrait;
class RedirectManagerTest extends BrowserTestBase {
  use CronRunTrait;
  use Domain301RedirectFunctionalTestTrait;
  protected static $modules = [
    'domain_301_redirect',
  ];
  protected $domain;
  protected $notTheDomain;
  
  protected $configFactory;
  
  protected $redirectManager;
  
  protected function setUp() {
    $test_url = getenv('SIMPLETEST_BASE_URL');
    $parsed_url = parse_url($test_url);
    if (!isset($parsed_url['host'])) {
      $this
        ->markTestSkipped('Missing SIMPLETEST_BASE_URL');
      return;
    }
    $host = trim($parsed_url['host'], '/');
    $this->domain = "http://{$host}/";
    $this->notTheDomain = "http://not.{$host}/";
    parent::setUp();
    $this->configFactory = Drupal::service('config.factory');
    $this->redirectManager = Drupal::service('domain_301_redirect.manager');
  }
  
  public function testCheckDomain() {
    $isDomainOk = $this->redirectManager
      ->checkDomain($this->domain);
    $this
      ->assertTrue($isDomainOk, 'The domain resolves.');
    $this
      ->enableRedirect($this->notTheDomain);
    $isDomainOk = $this->redirectManager
      ->checkDomain($this->notTheDomain);
    list($statusCode, ) = $this
      ->getRedirect($this->domain);
    $this
      ->assertEquals(301, $statusCode, 'The site is redirecting.');
    $this
      ->assertFalse($isDomainOk, 'The redirect domain does not resolve.');
    
    Drupal::service('cron')
      ->run();
    $this->configFactory
      ->clearStaticCache();
    $config = $this->configFactory
      ->get('domain_301_redirect.settings');
    $enabled = $config
      ->get('enabled');
    $this
      ->assertEquals(0, $enabled, 'The 301 redirect is no longer enabled.');
    
    $this
      ->drupalGet('<front>');
    $statusCode = $this
      ->getSession()
      ->getStatusCode();
    $this
      ->assertEquals(200, $statusCode, 'The site is not redirecting.');
  }
}