Skip to content

Commit f179717

Browse files
committed
Discovery: Test the cache TTL.
1 parent 83fe4b5 commit f179717

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tests/src/Kernel/DiscoveryFetcherTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Drupal\Core\Logger\RfcLogLevel;
2222
use Drupal\KernelTests\KernelTestBase;
2323
use Drupal\Tests\collabora_online\Traits\KernelTestLoggerTrait;
24+
use Drupal\Tests\collabora_online\Traits\KernelTestTimeTrait;
2425
use GuzzleHttp\Client;
2526
use GuzzleHttp\Exception\TransferException;
2627
use GuzzleHttp\Psr7\Response;
@@ -31,6 +32,7 @@
3132
class DiscoveryFetcherTest extends KernelTestBase {
3233

3334
use KernelTestLoggerTrait;
35+
use KernelTestTimeTrait;
3436

3537
/**
3638
* XML content to be returned for a discovery request.
@@ -158,9 +160,38 @@ public function testGetDiscoveryIsCached(): void {
158160
->save();
159161

160162
$fetcher->getDiscovery();
163+
$this->assertCount(3, $this->httpClientGetCalls);
161164
$cache_record = $load_cache();
162165
$this->assertNotFalse($cache_record);
163166
$this->assertSame(12345, $cache_record->expire - $cache_record->created);
167+
168+
$this->mockRequestTime = $this->mockRequestTime->add(new \DateInterval('PT12330S'));
169+
170+
$this->assertSame(12330, $this->mockRequestTime->getTimestamp() - $cache_record->created);
171+
$this->assertNotFalse($load_cache());
172+
173+
$fetcher->getDiscovery();
174+
$this->assertCount(3, $this->httpClientGetCalls);
175+
176+
$this->mockRequestTime = $this->mockRequestTime->add(new \DateInterval('PT30S'));
177+
178+
$this->assertFalse($load_cache());
179+
$this->assertSame(12360, $this->mockRequestTime->getTimestamp() - $cache_record->created);
180+
181+
$fetcher->getDiscovery();
182+
$this->assertCount(4, $this->httpClientGetCalls);
183+
184+
// Set TTL = 0 for no cache, always refresh.
185+
$this->config('collabora_online.settings')
186+
->set('cool.discovery_cache_ttl', 0)
187+
->save();
188+
$fetcher->getDiscovery();
189+
$this->assertCount(5, $this->httpClientGetCalls);
190+
$fetcher->getDiscovery();
191+
$this->assertCount(5, $this->httpClientGetCalls);
192+
$this->mockRequestTime = $this->mockRequestTime->add(new \DateInterval('PT1S'));
193+
$fetcher->getDiscovery();
194+
$this->assertCount(6, $this->httpClientGetCalls);
164195
}
165196

166197
/**

0 commit comments

Comments
 (0)