Commit baf26525 authored by Derek Wright's avatar Derek Wright Committed by Sascha Grossenbacher
Browse files

Issue #2958635 by dww, mpp, mariacha1, VladimirAus: Restore link to add a...

Issue #2958635 by dww, mpp, mariacha1, VladimirAus: Restore link to add a redirect on node edit forms
parent c10c0884
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -500,5 +500,22 @@ function redirect_form_node_form_alter(&$form, FormStateInterface $form_state, $
        '#suffix' => '</p>',
      ];
    }

    $form['url_redirects']['actions'] = [
      '#theme' => 'links',
      '#links' => [],
      '#attributes' => ['class' => ['action-links']],
    ];
    $form['url_redirects']['actions']['#links']['add'] = [
      'title' => t('Add URL redirect'),
      'url' => Url::fromRoute('redirect.add', [
        'redirect' => $node->toUrl()->getInternalPath(),
        'destination' => \Drupal::destination()->get(),
      ]),
      'attributes' => [
        'class' => 'button',
        'target' => '_blank',
      ],
    ];
  }
}
+95 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\redirect\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Test the redirect functionality on node forms.
 *
 * @group redirect
 */
class RedirectNodeFormTest extends BrowserTestBase {

  /**
   * A normal logged in user.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $webUser;

  /**
   * A user with permission to bypass content access checks, and add redirects.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['node', 'redirect'];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Create Basic page node type.
    if ($this->profile != 'standard') {
      $this->drupalCreateContentType([
        'type' => 'page',
        'name' => 'Basic page',
        'display_submitted' => FALSE,
      ]);
    }

    $this->webUser = $this->drupalCreateUser([
      'access content',
      'create page content',
      'edit own page content',
    ]);

    $this->adminUser = $this->drupalCreateUser([
      'access content',
      'administer redirects',
      'bypass node access',
    ]);
  }

  /**
   * Test redirect functionality on the node edit form.
   */
  public function testNodeForm() {
    // Login as a regular user.
    $this->drupalLogin($this->webUser);

    // Create "Basic page" content with title.
    $settings = [
      'title' => $this->randomMachineName(8),
    ];
    $node = $this->drupalCreateNode($settings);

    // Load the node edit form.
    $this->drupalGet('node/' . $node->id() . '/edit');

    // Make sure the redirect add button is not visible to this regular user.
    $this->assertNoRaw('Add URL redirect');

    // Now edit the same node as an admin user.
    $this->drupalLogin($this->adminUser);
    $this->drupalGet('node/' . $node->id() . '/edit');

    // Make sure the redirect add button is visible for the admin user.
    $this->assertRaw('Add URL redirect');

    // Make sure the link works as expected.
    $this->clickLink('Add URL redirect');
    $this->assertUrl('admin/config/search/redirect/add');
    $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-redirect-redirect-0-uri']"), '/node/' . $node->id(), 'To: field correctly pre-filled.');
  }

}