Verified Commit 4792a5cc authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2927141 by Akhil Babu, pooja_sharma, geertvd, smustgrave, JeroenT,...

Issue #2927141 by Akhil Babu, pooja_sharma, geertvd, smustgrave, JeroenT, larowlan, catch, quietone: Updates to an entity's URL alias do not reflect on the corresponding local tasks

(cherry picked from commit 8448f2d1)
parent a8135412
Loading
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\LocalTaskManagerInterface;
@@ -87,6 +88,15 @@ public function build() {
    $config = $this->configuration;
    $cacheability = new CacheableMetadata();
    $cacheability->addCacheableDependency($this->localTaskManager);
    // If the current route belongs to an entity, include cache tags of that
    // entity as well.
    $route_parameters = $this->routeMatch->getParameters()->all();
    foreach ($route_parameters as $parameter) {
      if ($parameter instanceof CacheableDependencyInterface) {
        $cacheability->addCacheableDependency($parameter);
      }
    }

    $tabs = [
      '#theme' => 'menu_local_tasks',
    ];
+1 −1
Original line number Diff line number Diff line
@@ -415,7 +415,7 @@ public function testNodeFieldTranslation() {
    $this->assertSession()->pageTextContains('Successfully saved French translation.');

    // Check that the translations are saved.
    $this->clickLink('Add');
    $this->clickLink('Edit');
    $this->assertSession()->responseContains('FR label');
  }

+67 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;

// cspell:ignore ragdoll

@@ -17,6 +18,8 @@
 */
class LocalTasksTest extends BrowserTestBase {

  use TaxonomyTestTrait;

  /**
   * Modules to enable.
   *
@@ -302,4 +305,68 @@ public function testLocalTaskBlockCache() {
    ]);
  }

  /**
   * Tests local task block URLs for entities with path aliases.
   */
  public function testLocalTaskBlockUrl(): void {
    // Install the necessary modules for the test.
    \Drupal::service('module_installer')->install(['path', 'taxonomy']);
    $this->drupalCreateContentType(['type' => 'article']);
    $vocab = $this->createVocabulary(['vid' => 'tags']);

    $web_user = $this->drupalCreateUser([
      'create article content',
      'edit own article content',
      'create url aliases',
      'create terms in tags',
      'edit terms in tags',
    ]);

    // Create node and taxonomy term entities with path aliases.
    $entities = [
      'node' => $this->drupalCreateNode([
        'type' => 'article',
        'path' => [
          'alias' => '/original-node-alias',
        ],
        'uid' => $web_user->id(),
      ]),
      'term' => $this->createTerm($vocab, [
        'path' => [
          'alias' => '/original-term-alias',
        ],
        'uid' => $web_user->id(),
      ]),
    ];

    $this->drupalLogin($web_user);
    // Test the local task block URLs for both node and term entities.
    foreach ($entities as $entity_type => $entity) {
      $this->drupalGet($entity->toUrl());
      $this->assertSameLocalTaskUrl('/original-' . $entity_type . '-alias');

      $this->drupalGet($entity->toUrl('edit-form'));
      $new_alias = '/original-' . $entity_type . '-alias-updated';
      $edit = ['path[0][alias]' => $new_alias];
      $this->submitForm($edit, 'Save');

      $this->assertSameLocalTaskUrl($new_alias);
      $this->drupalGet($entity->toUrl('edit-form'));
      $this->assertSameLocalTaskUrl($new_alias);
    }
  }

  /**
   * Asserts that the local task URL matches the expected alias.
   *
   * @param string $alias
   *   The expected path alias.
   */
  protected function assertSameLocalTaskUrl(string $alias): void {
    // Assert that the href attribute of the 'View' link contains the expected
    // alias.
    $link = $this->assertSession()->elementExists('xpath', '//a[text()="View"]');
    $this->assertStringContainsString($alias, $link->getAttribute('href'));
  }

}