Skip to content

Commit

Permalink
Merge pull request #26 from wunderio/bugfix/refresh
Browse files Browse the repository at this point in the history
Refactor refresh()
  • Loading branch information
ragnarkurmwunder authored Apr 27, 2023
2 parents c732729 + 00edc73 commit 81363e1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/UpdatesLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,16 @@ public function logDiff(array $statuses): void {
* Update module statuses, get the fresh data from internet.
*
* Ripped from update_cron().
* Note, that the refresh is quite tricky.
* It is easy to clash with update_cron() and get into broken state.
* See also: https://www.drupal.org/project/drupal/issues/2920285
*/
public function refresh(): void {

$last_check = $this->state->get('update.last_check', 0);
$request_time = \Drupal::time()->getRequestTime();
if ($request_time - $last_check > 1 * 60 * 60) {
return;
}
$this->updateManager->refreshUpdateData();
$this->updateProcessor->fetchData();
update_clear_update_disk_cache();
Expand Down
68 changes: 68 additions & 0 deletions tests/src/Kernel/RefreshTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Drupal\Tests\updates_log\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\updates_log\UpdatesLog;

/**
* Test refresh handling.
*
* @group updates_log
*/
class RefreshTest extends KernelTestBase {

/**
* The UpdatesLog service.
*
* @var \Drupal\updates_log\UpdatesLog
*/
private UpdatesLog $updatesLogService;

/**
* The modules to load to run the test.
*
* @var array
*/
protected static $modules = [
'update',
'updates_log',
];

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['updates_log']);
$this->installConfig(['update']);
$this->updatesLogService = \Drupal::service('updates_log.updates_logger');
}

/**
* @covers ::refresh
*/
public function testRefreshOld(): void {

\Drupal::state()->set('update.last_check', 0);

$this->updatesLogService->refresh();
$statuses = $this->updatesLogService->statusesGet();

$this->assertGreaterThan(0, count($statuses));
}

/**
* @covers ::refresh
*/
public function testRefreshNew(): void {

\Drupal::state()->set('update.last_check', time());

$this->updatesLogService->refresh();
$statuses = $this->updatesLogService->statusesGet();

$this->assertGreaterThan(0, count($statuses));
}

}
5 changes: 4 additions & 1 deletion tests/src/Kernel/UpdatesLogDisabledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ protected function setUp(): void {
* Test that there is no output when disabled = TRUE.
*/
public function testDisabledDoesNotRun(): void {
new Settings(['updates_log_disabled' => TRUE, 'hash_salt' => 'notsosecurehash']);
new Settings([
'updates_log_disabled' => TRUE,
'hash_salt' => 'notsosecurehash',
]);
$this->updatesLogService->run();
$query = $this->db->query("select * from {watchdog}");
$result = $query->fetchAll();
Expand Down
2 changes: 1 addition & 1 deletion updates_log.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ description: "Log module update info"
core: 8.x
core_version_requirement: ^8 || ^9 || ^10
package: Development
version: 2.3.0
version: 2.3.1
dependencies:
- drupal:update

0 comments on commit 81363e1

Please sign in to comment.