diff --git a/src/EnabledSourceHandler.php b/src/EnabledSourceHandler.php index d1372f36af551be06e2fcfc5eed62a9b25bc40ee..9ee083e76fcf0b8c4c01c1b3deb82d56c90171a0 100644 --- a/src/EnabledSourceHandler.php +++ b/src/EnabledSourceHandler.php @@ -134,15 +134,17 @@ class EnabledSourceHandler implements LoggerAwareInterface, EventSubscriberInter // never store it. $this->getActivationData($project); } - // Store the results for this query as a set of arguments to - // ProjectsResultsPage. - $storage->set($cache_key, [ - $results->totalResults, - array_column($results->list, 'id'), - $results->pluginLabel, - $source_id, - $results->error, - ]); + // If there were no query errors, store the results as a set of arguments + // to ProjectsResultsPage. + if (empty($results->error)) { + $storage->set($cache_key, [ + $results->totalResults, + array_column($results->list, 'id'), + $results->pluginLabel, + $source_id, + $results->error, + ]); + } } return [$source_id => $results]; } diff --git a/tests/modules/project_browser_test/src/Plugin/ProjectBrowserSource/ProjectBrowserTestMock.php b/tests/modules/project_browser_test/src/Plugin/ProjectBrowserSource/ProjectBrowserTestMock.php index bfd5fea1900a1b96e830e117b18cea942dace920..c74a5a2995cc0e7062a3704134f0bb71121e9ff3 100644 --- a/tests/modules/project_browser_test/src/Plugin/ProjectBrowserSource/ProjectBrowserTestMock.php +++ b/tests/modules/project_browser_test/src/Plugin/ProjectBrowserSource/ProjectBrowserTestMock.php @@ -54,6 +54,13 @@ class ProjectBrowserTestMock extends ProjectBrowserSourceBase { */ const MAINTAINED_VALUES = [13028, 19370, 9990]; + /** + * An error message to flag when querying. + * + * @var string|null + */ + public static ?string $resultsError = NULL; + /** * Constructor for mock API. * @@ -393,7 +400,7 @@ class ProjectBrowserTestMock extends ProjectBrowserSourceBase { } } - return $this->createResultsPage($returned_list, $api_response['total_results'] ?? 0); + return $this->createResultsPage($returned_list, $api_response['total_results'] ?? 0, static::$resultsError); } /** diff --git a/tests/src/Functional/EnabledSourceHandlerTest.php b/tests/src/Functional/EnabledSourceHandlerTest.php index 1b587717038728659b24b6af1dfcb53a6b400d99..dd18731dbe0870c2c48e2cba4f6e50eca2892f3f 100644 --- a/tests/src/Functional/EnabledSourceHandlerTest.php +++ b/tests/src/Functional/EnabledSourceHandlerTest.php @@ -6,6 +6,7 @@ namespace Drupal\Tests\project_browser\Functional; use Drupal\project_browser\EnabledSourceHandler; use Drupal\project_browser\ProjectBrowser\Project; +use Drupal\project_browser_test\Plugin\ProjectBrowserSource\ProjectBrowserTestMock; use Drupal\Tests\BrowserTestBase; /** @@ -87,6 +88,29 @@ class EnabledSourceHandlerTest extends BrowserTestBase { $this->assertFalse(self::hasActivationData($project)); } + /** + * Tests that query results are not stored if there was an error. + */ + public function testErrorsAreNotStored(): void { + /** @var \Drupal\project_browser\EnabledSourceHandler $handler */ + $handler = $this->container->get(EnabledSourceHandler::class); + + $handler->getProjects('project_browser_test_mock'); + /** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $storage */ + $storage = $this->container->get('keyvalue') + ->get('project_browser:project_browser_test_mock'); + // Query results should have been stored. + $query_cache_key = 'query:' . md5('[]'); + $this->assertTrue($storage->has($query_cache_key)); + + $handler->clearStorage(); + ProjectBrowserTestMock::$resultsError = 'Nope!'; + + $handler->getProjects('project_browser_test_mock'); + // No query results should have been stored. + $this->assertFalse($storage->has($query_cache_key)); + } + /** * Checks if a project object is carrying activation data. *