From 2f0d1d9e0175f2115a910663deddfc9b5976c946 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 7 Oct 2024 16:20:12 +0300 Subject: [PATCH] UHF-10581: test errors --- .../tests/src/Kernel/IndexingTest.php | 50 ++++++++++++++++++- .../tests/src/MockIndexingClass.php | 0 2 files changed, 48 insertions(+), 2 deletions(-) delete mode 100644 public/modules/custom/helfi_google_api/tests/src/MockIndexingClass.php diff --git a/public/modules/custom/helfi_google_api/tests/src/Kernel/IndexingTest.php b/public/modules/custom/helfi_google_api/tests/src/Kernel/IndexingTest.php index 69a017fd..daf364ff 100644 --- a/public/modules/custom/helfi_google_api/tests/src/Kernel/IndexingTest.php +++ b/public/modules/custom/helfi_google_api/tests/src/Kernel/IndexingTest.php @@ -6,10 +6,14 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Routing\UrlGeneratorInterface; +use Drupal\helfi_google_api\GoogleApi; use Drupal\helfi_google_api\JobIndexingService; +use Drupal\helfi_google_api\Response; use Drupal\path_alias\AliasManagerInterface; use Drupal\redirect\Entity\Redirect; use Drupal\Tests\helfi_api_base\Functional\ExistingSiteTestBase; +use Google\Service\Indexing; +use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; /** @@ -57,6 +61,29 @@ public function testIndexingJoblisting(): void { $this->assertTrue(str_contains($indexed_url, $expected)); } + public function testIndexingErrors() { + $random = rand(1000, 9999); + $recruitmentId = "TESTI-1234-56-$random"; + $timestamp = time() - 1; + + $node = $this->createNode([ + 'type' => 'job_listing', + 'langcode' => 'sv', + 'title' => 'en jobb', + 'field_recruitment_id' => $recruitmentId, + 'publish_on' => $timestamp, + ]); + + /** @var \Drupal\helfi_google_api\JobIndexingService $indexingService */ + $indexingService = $this->getSut('errors'); + + /** @var \Drupal\helfi_google_api\Response $response */ + $response = $indexingService->indexEntity($node); + + $this->assertCount(1, $response->getErrors()); + $this->assertCount(1, $response->getUrls()); + } + /** * Test deindexing. */ @@ -109,8 +136,27 @@ public function testDeindexing(): void { * @return \Drupal\helfi_google_api\JobIndexingService * The job indexing service. */ - private function getSut(): JobIndexingService { - $googleApi = $this->container->get('Drupal\helfi_google_api\GoogleApi'); + private function getSut($errors = ''): JobIndexingService { + if ($errors === 'errors') { + $googleApi = $this->prophesize(GoogleApi::class); + $googleApi->isDryRun() + ->willReturn(TRUE); + + $response = new Response( + ['https://test.fi/url'], + ['Unable to verify url ownership'], + TRUE + ); + $googleApi->indexBatch(Argument::any(), Argument::any()) + ->willReturn($response); + $googleApi = $googleApi->reveal(); + } + else { + $googleApi = new GoogleApi( + $this->container->get('config.factory'), + $this->prophesize(Indexing::class)->reveal() + ); + } $entityTypeManager = $this->container->get(EntityTypeManagerInterface::class); $aliasManager = $this->container->get(AliasManagerInterface::class); $urlGenerator = $this->container->get(UrlGeneratorInterface::class); diff --git a/public/modules/custom/helfi_google_api/tests/src/MockIndexingClass.php b/public/modules/custom/helfi_google_api/tests/src/MockIndexingClass.php deleted file mode 100644 index e69de29b..00000000