From f32b70339aed678518d8e38bbabadc09c4f0c9bc Mon Sep 17 00:00:00 2001
From: prashantdsala <p1989chauhan@gmail.com>
Date: Tue, 22 Apr 2025 16:15:56 +0530
Subject: [PATCH 1/4] Rebuild route in Search form base

---
 .../search/src/Form/SearchPageFormBase.php    | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/core/modules/search/src/Form/SearchPageFormBase.php b/core/modules/search/src/Form/SearchPageFormBase.php
index a0df9cbee015..88e596a343af 100644
--- a/core/modules/search/src/Form/SearchPageFormBase.php
+++ b/core/modules/search/src/Form/SearchPageFormBase.php
@@ -5,6 +5,7 @@
 use Drupal\Core\Entity\EntityForm;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Plugin\PluginFormInterface;
+use Drupal\Core\Routing\RouteBuilderInterface;
 use Drupal\search\SearchPageRepositoryInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
@@ -34,14 +35,24 @@ abstract class SearchPageFormBase extends EntityForm {
    */
   protected $searchPageRepository;
 
+  /**
+   * The router builder.
+   *
+   * @var \Drupal\Core\Routing\RouteBuilderInterface
+   */
+  protected $routerBuilder;
+
   /**
    * Constructs a new search form.
    *
    * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository
    *   The search page repository.
+   * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
+   *   The router builder.
    */
-  public function __construct(SearchPageRepositoryInterface $search_page_repository) {
+  public function __construct(SearchPageRepositoryInterface $search_page_repository, RouteBuilderInterface $router_builder) {
     $this->searchPageRepository = $search_page_repository;
+    $this->routerBuilder = $router_builder;
   }
 
   /**
@@ -49,7 +60,8 @@ public function __construct(SearchPageRepositoryInterface $search_page_repositor
    */
   public static function create(ContainerInterface $container) {
     return new static(
-      $container->get('search.search_page_repository')
+      $container->get('search.search_page_repository'),
+      $container->get('router.builder')
     );
   }
 
@@ -162,7 +174,8 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    */
   public function save(array $form, FormStateInterface $form_state) {
     $this->entity->save();
-
+    // Rebuild routes to ensure the new search page route is available immediately.
+    $this->routerBuilder->rebuildIfNeeded();
     $form_state->setRedirectUrl($this->entity->toUrl('collection'));
   }
 
-- 
GitLab


From 95b9bb21edd6e8c67ee1bd1e5b622846dc97cbd9 Mon Sep 17 00:00:00 2001
From: prashantdsala <p1989chauhan@gmail.com>
Date: Thu, 24 Apr 2025 11:38:11 +0530
Subject: [PATCH 2/4] Construct property promotion

---
 .../search/src/Form/SearchPageFormBase.php    | 26 +++++--------------
 .../search/src/SearchPageRepository.php       | 19 +++++---------
 2 files changed, 13 insertions(+), 32 deletions(-)

diff --git a/core/modules/search/src/Form/SearchPageFormBase.php b/core/modules/search/src/Form/SearchPageFormBase.php
index 88e596a343af..32516a5da35a 100644
--- a/core/modules/search/src/Form/SearchPageFormBase.php
+++ b/core/modules/search/src/Form/SearchPageFormBase.php
@@ -28,32 +28,18 @@ abstract class SearchPageFormBase extends EntityForm {
    */
   protected $plugin;
 
-  /**
-   * The search page repository.
-   *
-   * @var \Drupal\search\SearchPageRepositoryInterface
-   */
-  protected $searchPageRepository;
-
-  /**
-   * The router builder.
-   *
-   * @var \Drupal\Core\Routing\RouteBuilderInterface
-   */
-  protected $routerBuilder;
-
   /**
    * Constructs a new search form.
    *
-   * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository
+   * @param \Drupal\search\SearchPageRepositoryInterface $searchPageRepository
    *   The search page repository.
-   * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
+   * @param \Drupal\Core\Routing\RouteBuilderInterface $routerBuilder
    *   The router builder.
    */
-  public function __construct(SearchPageRepositoryInterface $search_page_repository, RouteBuilderInterface $router_builder) {
-    $this->searchPageRepository = $search_page_repository;
-    $this->routerBuilder = $router_builder;
-  }
+  public function __construct(
+    protected SearchPageRepositoryInterface $searchPageRepository,
+    protected RouteBuilderInterface $routerBuilder,
+  ) {}
 
   /**
    * {@inheritdoc}
diff --git a/core/modules/search/src/SearchPageRepository.php b/core/modules/search/src/SearchPageRepository.php
index 97606d42a027..e6b20f4bd49b 100644
--- a/core/modules/search/src/SearchPageRepository.php
+++ b/core/modules/search/src/SearchPageRepository.php
@@ -10,13 +10,6 @@
  */
 class SearchPageRepository implements SearchPageRepositoryInterface {
 
-  /**
-   * The config factory.
-   *
-   * @var \Drupal\Core\Config\ConfigFactoryInterface
-   */
-  protected $configFactory;
-
   /**
    * The search page storage.
    *
@@ -27,14 +20,16 @@ class SearchPageRepository implements SearchPageRepositoryInterface {
   /**
    * Constructs a new SearchPageRepository.
    *
-   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
+   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
    *   The config factory.
-   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
+   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
    *   The entity type manager.
    */
-  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
-    $this->configFactory = $config_factory;
-    $this->storage = $entity_type_manager->getStorage('search_page');
+  public function __construct(
+    protected ConfigFactoryInterface $configFactory,
+    protected EntityTypeManagerInterface $entityTypeManager,
+  ) {
+    $this->storage = $this->entityTypeManager->getStorage('search_page');
   }
 
   /**
-- 
GitLab


From 8cf35e37362dce3399dafe4d1b08ff785673eb8c Mon Sep 17 00:00:00 2001
From: prashantdsala <p1989chauhan@gmail.com>
Date: Thu, 24 Apr 2025 12:20:00 +0530
Subject: [PATCH 3/4] Functional test coverage to add search page of type
 content

---
 .../tests/src/Functional/SearchPageTest.php   | 68 +++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 core/modules/search/tests/src/Functional/SearchPageTest.php

diff --git a/core/modules/search/tests/src/Functional/SearchPageTest.php b/core/modules/search/tests/src/Functional/SearchPageTest.php
new file mode 100644
index 000000000000..316de7a16b60
--- /dev/null
+++ b/core/modules/search/tests/src/Functional/SearchPageTest.php
@@ -0,0 +1,68 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Drupal\Tests\search\Functional;
+
+use Drupal\Tests\BrowserTestBase;
+
+/**
+ * Tests the search page creation and verification.
+ *
+ * @group search
+ */
+class SearchPageTest extends BrowserTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $modules = ['node', 'search'];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected $defaultTheme = 'stark';
+
+  /**
+   * Tests creating a new search page and verifying it works.
+   */
+  public function testSearchPageCreation() {
+    // Log in as admin.
+    $this->drupalLogin($this->drupalCreateUser([
+      'search content',
+      'administer search',
+      'use advanced search',
+      'access user profiles',
+    ]));
+
+    // Visit the search pages configuration page.
+    $this->drupalGet('admin/config/search/pages');
+    $this->assertSession()->statusCodeEquals(200);
+
+    // Select "Content" from the search page type dropdown.
+    $page = $this->getSession()->getPage();
+    $page->fillField('search_type', 'node_search');
+
+    // Click "Add Search Page" button.
+    $this->getSession()->getPage()->pressButton('edit-add-search-submit');
+    $this->assertSession()->statusCodeEquals(200);
+
+    // Fill in the form fields.
+    $this->getSession()->getPage()->fillField('Label', 'Test page');
+    $this->getSession()->getPage()->fillField('Machine-readable name', 'test_page');
+
+    $this->getSession()->getPage()->fillField('Path', 'test-page');
+
+    // Save the form.
+    $this->getSession()->getPage()->pressButton('Save');
+    $this->assertSession()->statusCodeEquals(200);
+
+    // Verify we are redirected back to the search pages list.
+    $this->assertSession()->addressEquals('admin/config/search/pages');
+
+    // Visit the new search page to verify it exists.
+    $this->drupalGet('search/test-page');
+    $this->assertSession()->statusCodeEquals(200);
+  }
+
+}
-- 
GitLab


From 73956f1447f5309c0691fa601f94cf0ff99711eb Mon Sep 17 00:00:00 2001
From: prashantdsala <p1989chauhan@gmail.com>
Date: Thu, 24 Apr 2025 12:26:20 +0530
Subject: [PATCH 4/4] phpstan issue fix

---
 core/modules/search/tests/src/Functional/SearchPageTest.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/core/modules/search/tests/src/Functional/SearchPageTest.php b/core/modules/search/tests/src/Functional/SearchPageTest.php
index 316de7a16b60..e426c8e660cf 100644
--- a/core/modules/search/tests/src/Functional/SearchPageTest.php
+++ b/core/modules/search/tests/src/Functional/SearchPageTest.php
@@ -26,7 +26,7 @@ class SearchPageTest extends BrowserTestBase {
   /**
    * Tests creating a new search page and verifying it works.
    */
-  public function testSearchPageCreation() {
+  public function testSearchPageCreation(): void {
     // Log in as admin.
     $this->drupalLogin($this->drupalCreateUser([
       'search content',
-- 
GitLab

