View source  
  <?php
namespace Drupal\Tests\redirect_404\Functional;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Url;
class Fix404RedirectUITest extends Redirect404TestBase {
  
  public function testFix404Pages() {
    
    $this
      ->drupalGet('non-existing0');
    
    $this
      ->drupalGet('admin/config/search/redirect/404');
    $this
      ->assertText('non-existing0');
    $this
      ->clickLink(t('Add redirect'));
    
    $destination = Url::fromRoute('redirect_404.fix_404')
      ->getInternalPath();
    $expected_query = [
      'destination' => $destination,
      'language' => 'en',
      'source' => 'non-existing0',
    ];
    $parsed_url = UrlHelper::parse($this
      ->getUrl());
    $this
      ->assertEqual(Url::fromRoute('redirect.add')
      ->setAbsolute()
      ->toString(), $parsed_url['path']);
    $this
      ->assertEqual($expected_query, $parsed_url['query']);
    $this
      ->assertFieldByName('redirect_source[0][path]', 'non-existing0');
    
    $edit = [
      'redirect_redirect[0][uri]' => '/node',
    ];
    $this
      ->drupalPostForm(NULL, $edit, t('Save'));
    $this
      ->assertUrl('admin/config/search/redirect/404');
    $this
      ->assertText('There are no 404 errors to fix.');
    
    $this
      ->drupalGet('non-existing0');
    $this
      ->assertUrl('node');
    
    $this
      ->drupalGet('admin/config/search/redirect');
    $this
      ->assertText('non-existing0');
    $this
      ->clickLink('Delete', 0);
    $this
      ->drupalPostForm(NULL, [], 'Delete');
    $this
      ->assertUrl('admin/config/search/redirect');
    $this
      ->assertText('There is no redirect yet.');
    $this
      ->drupalGet('admin/config/search/redirect/404');
    $this
      ->assertText('There are no 404 errors to fix.');
    
    $this
      ->drupalGet('non-existing0');
    $this
      ->drupalGet('admin/config/search/redirect/404');
    $this
      ->assertText('non-existing0');
    
    $this
      ->drupalGet('non-existing0?test=1');
    $this
      ->drupalGet('non-existing0?test=2');
    $this
      ->drupalGet('non-existing1');
    $this
      ->drupalGet('non-existing2');
    $this
      ->drupalGet('admin/config/search/redirect/404');
    $this
      ->assertText('non-existing0?test=1');
    $this
      ->assertText('non-existing0?test=2');
    $this
      ->assertText('non-existing0');
    $this
      ->assertText('non-existing1');
    $this
      ->assertText('non-existing2');
    
    $this
      ->drupalGet('admin/config/search/redirect/404', [
      'query' => [
        'path' => 'test=',
      ],
    ]);
    $this
      ->assertText('non-existing0?test=1');
    $this
      ->assertText('non-existing0?test=2');
    $this
      ->assertNoText('non-existing1');
    $this
      ->assertNoText('non-existing2');
    $this
      ->drupalGet('admin/config/search/redirect/404', [
      'query' => [
        'path' => 'existing1',
      ],
    ]);
    $this
      ->assertNoText('non-existing0?test=1');
    $this
      ->assertNoText('non-existing0?test=2');
    $this
      ->assertNoText('non-existing0');
    $this
      ->assertText('non-existing1');
    $this
      ->assertNoText('non-existing2');
    $this
      ->drupalGet('admin/config/search/redirect/404');
    $this
      ->assertText('non-existing0?test=1');
    $this
      ->assertText('non-existing0?test=2');
    $this
      ->assertText('non-existing0');
    $this
      ->assertText('non-existing1');
    $this
      ->assertText('non-existing2');
    $this
      ->drupalGet('admin/config/search/redirect/404', [
      'query' => [
        'path' => 'g2',
      ],
    ]);
    $this
      ->assertNoText('non-existing0?test=1');
    $this
      ->assertNoText('non-existing0?test=2');
    $this
      ->assertNoText('non-existing0');
    $this
      ->assertNoText('non-existing1');
    $this
      ->assertText('non-existing2');
    
    $this
      ->clickLink('Add redirect');
    $expected_query = [
      'source' => 'non-existing2',
      'language' => 'en',
      'destination' => $destination,
    ];
    $parsed_url = UrlHelper::parse($this
      ->getUrl());
    $this
      ->assertEqual(Url::fromRoute('redirect.add')
      ->setAbsolute()
      ->toString(), $parsed_url['path']);
    $this
      ->assertEqual($expected_query, $parsed_url['query']);
    $this
      ->assertFieldByName('redirect_source[0][path]', 'non-existing2');
    $this
      ->drupalPostForm(NULL, $edit, t('Save'));
    $this
      ->assertUrl('admin/config/search/redirect/404');
    $this
      ->assertText('non-existing0?test=1');
    $this
      ->assertText('non-existing0?test=2');
    $this
      ->assertText('non-existing0');
    $this
      ->assertText('non-existing1');
    $this
      ->assertNoText('non-existing2');
    
    $this
      ->drupalGet('admin/config/search/redirect');
    $this
      ->assertText('non-existing2');
  }
  
  public function testIgnorePages() {
    
    $node1 = $this
      ->drupalCreateNode([
      'type' => 'page',
    ]);
    $node2 = $this
      ->drupalCreateNode([
      'type' => 'page',
    ]);
    
    $node_to_ignore = '/node/' . $node1
      ->id() . '/test';
    $terms_to_ignore = '/term/*';
    $pages = $node_to_ignore . "\r\n" . $terms_to_ignore . "\n";
    \Drupal::configFactory()
      ->getEditable('redirect_404.settings')
      ->set('pages', $pages)
      ->save();
    
    $this
      ->drupalGet('node/' . $node1
      ->id() . '/test');
    $this
      ->drupalGet('term/foo');
    $this
      ->drupalGet('term/1');
    
    $this
      ->drupalGet('admin/config/search/redirect/404');
    $this
      ->assertNoText('node/' . $node1
      ->id() . '/test');
    $this
      ->assertNoText('term/foo');
    $this
      ->assertNoText('term/1');
    
    $this
      ->drupalGet('node/' . $node2
      ->id() . '/test');
    
    $this
      ->drupalGet('admin/config/search/redirect/404');
    $this
      ->assertText('node/' . $node2
      ->id() . '/test');
    
    $path_to_ignore = '/node/' . $node2
      ->id() . '/test';
    $destination = '&destination=admin/config/search/redirect/404';
    $this
      ->clickLink('Ignore');
    $this
      ->assertUrl('admin/config/search/redirect/settings?ignore=' . $path_to_ignore . $destination);
    $this
      ->assertText('Resolved the path ' . $path_to_ignore . ' in the database. Please check the ignored list and save the settings.');
    $this
      ->assertSession()
      ->fieldValueEquals('ignore_pages', $node_to_ignore . "\r\n/term/*\n/node/2/test");
    $this
      ->assertSession()
      ->elementContains('css', '#edit-ignore-pages', $node_to_ignore);
    $this
      ->assertSession()
      ->elementContains('css', '#edit-ignore-pages', $terms_to_ignore);
    $this
      ->assertSession()
      ->elementContains('css', '#edit-ignore-pages', $path_to_ignore);
    
    $nodes_to_ignore = 'node/*';
    $edit = [
      'ignore_pages' => $nodes_to_ignore . "\r\n" . $terms_to_ignore,
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Save configuration');
    
    $this
      ->assertUrl('admin/config/search/redirect/404');
    $this
      ->assertText('The configuration options have been saved.');
    $this
      ->assertNoText('node/' . $node2
      ->id() . '/test');
    $this
      ->assertText('There are no 404 errors to fix.');
    
    $this
      ->drupalGet('admin/config/search/redirect/settings');
    $xpath = $this
      ->xpath('//*[@id="edit-ignore-pages"]')[0]
      ->getHtml();
    
    $this
      ->assertSession()
      ->elementContains('css', '#edit-ignore-pages', '/' . $nodes_to_ignore);
    $this
      ->assertSession()
      ->elementContains('css', '#edit-ignore-pages', $terms_to_ignore);
    $this
      ->assertSession()
      ->elementNotContains('css', '#edit-ignore-pages', $node_to_ignore);
    $this
      ->assertSession()
      ->elementNotContains('css', '#edit-ignore-pages', $path_to_ignore);
    
    $this
      ->drupalGet('llama_page');
    $this
      ->drupalGet('admin/config/search/redirect/404');
    $this
      ->assertText('llama_page');
    $this
      ->clickLink('Ignore');
    $this
      ->assertSession()
      ->fieldValueEquals('ignore_pages', "/node/*\r\n/term/*\n/llama_page");
    $this
      ->getSession()
      ->getPage()
      ->pressButton('Save configuration');
    $this
      ->drupalGet('admin/config/search/redirect/settings');
    $this
      ->assertSession()
      ->fieldValueEquals('ignore_pages', "/node/*\r\n/term/*\n/llama_page");
  }
}