Verified Commit 32b49a77 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

fix: #3565886 Navigation module throws an error on missing URL

By: pjotr.savitski
By: libbna
By: sourav_paul
By: catch
By: diegodz
By: nicxvan
By: casey
By: berdir
By: plopesc
By: smustgrave
(cherry picked from commit acaf0b1c)
parent 53bea0b5
Loading
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -55,7 +55,12 @@ public function isContentEntityRoute(): bool {
  }

  public function getContentEntityFromRoute(): ?ContentEntityInterface {
    $path = $this->routeMatch->getRouteObject()->getPath();
    $route = $this->routeMatch->getRouteObject();
    if (!$route) {
      return NULL;
    }

    $path = $route->getPath();
    if (!$entity_type = $this->getContentEntityPaths()[$path] ?? NULL) {
      return NULL;
    }
+41 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\navigation\Kernel;

use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use Symfony\Component\HttpFoundation\Request;

/**
 * Tests \Drupal\navigation\EntityRouteHelper.
 *
 * @see \Drupal\navigation\EntityRouteHelper
 */
#[Group('navigation')]
#[RunTestsInSeparateProcesses]
class NavigationEntityRouteHelperTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'navigation',
    'layout_builder',
    'user',
  ];

  /**
   * Tests getContentEntityFromRoute() method when the route does not exist.
   */
  public function testGetContentEntityFromRouteWithNonExistentRoute(): void {
    $request = Request::create('/does-not-exist');
    $response = $this->container->get('http_kernel')->handle($request);
    $this->assertEquals(404, $response->getStatusCode());
    $this->assertNull($this->container->get('navigation.entity_route_helper')->getContentEntityFromRoute());
  }

}