Skip to content
Snippets Groups Projects
Commit 9ae97d18 authored by Ted Bowman's avatar Ted Bowman Committed by Adam G-H
Browse files

Issue #3269097 by tedbow, phenaproxima: Add test coverage to determine if...

Issue #3269097 by tedbow, phenaproxima: Add test coverage to determine if global functions are reloaded when updates are applied
parent f083a92b
No related branches found
No related tags found
1 merge request!232Issue #3269097: Add test coverage to determine if global functions are reloaded in postupdate
<?php
/**
* @file
* Contains global functions for testing updates to a .module file.
*/
/**
* A test function to test if global functions are reloaded during an update.
*
* @return string
*/
function _updated_module_global1(): string {
return "pre-update-value";
}
<?php
/**
* @file
* Contains global functions for testing updates to a .module file.
*/
/**
* A test function to test if global functions are reloaded during an update.
*
* @return string
*/
function _updated_module_global1(): string {
return "post-update-value";
}
/**
* A test function to test if a new global function will be available after an update.
*/
function _updated_module_global2(): void {
}
services:
package_manager_test_api.system_change_recorder:
class: Drupal\package_manager_test_api\SystemChangeRecorder
arguments:
- '@package_manager.path_locator'
- '@state'
tags:
- { name: event_subscriber }
<?php
namespace Drupal\package_manager_test_api;
use Drupal\Core\State\StateInterface;
use Drupal\package_manager\Event\PostApplyEvent;
use Drupal\package_manager\Event\PostDestroyEvent;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Event\StageEvent;
use Drupal\package_manager\PathLocator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Defines a service for checking system changes during an update.
*/
class SystemChangeRecorder implements EventSubscriberInterface {
/**
* The path locator service.
*
* @var \Drupal\package_manager\PathLocator
*/
private $pathLocator;
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
private $state;
/**
* Constructs a SystemChangeRecorder object.
*
* @param \Drupal\package_manager\PathLocator $path_locator
* The path locator service.
* @param \Drupal\Core\State\StateInterface $state
* The state service.
*/
public function __construct(PathLocator $path_locator, StateInterface $state) {
$this->pathLocator = $path_locator;
$this->state = $state;
}
/**
* Records aspects of system state at various points during an update.
*
* @param \Drupal\package_manager\Event\StageEvent $event
* The stage event.
*/
public function recordSystemState(StageEvent $event): void {
$results = [];
// Call a function in a loaded file to ensure it doesn't get reloaded after
// changes are applied.
$results['return value of existing global function'] = _updated_module_global1();
// Check if a new global function exists after changes are applied.
$results['new global function exists'] = function_exists('_update_module_global2') ? "exists" : "not exists";
$phase = $event instanceof PreApplyEvent ? 'pre' : 'post';
$this->state->set("system_changes:$phase", $results);
}
/**
* Writes the results of ::recordSystemState() to file.
*
* Build tests do not have access to the Drupal API, so write the results to
* a file so the build test can check them.
*/
public function writeResultsToFile(): void {
$results = [
'pre' => $this->state->get('system_changes:pre'),
'post' => $this->state->get('system_changes:post'),
];
$dir = $this->pathLocator->getProjectRoot();
file_put_contents("$dir/system_changes.json", json_encode($results));
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
PreApplyEvent::class => 'recordSystemState',
PostApplyEvent::class => 'recordSystemState',
PostDestroyEvent::class => 'writeResultsToFile',
];
}
}
......@@ -43,6 +43,7 @@ class PackageUpdateTest extends TemplateProjectTestBase {
'web/modules/contrib/alpha/composer.json',
'web/modules/contrib/updated_module/composer.json',
'bravo.txt',
"system_changes.json",
],
]);
$this->visit("/package-manager-test-api?$query");
......@@ -65,6 +66,21 @@ class PackageUpdateTest extends TemplateProjectTestBase {
// created this file.
// @see \Drupal\updated_module\PostApplySubscriber::postApply()
$this->assertSame('Bravo!', $file_contents['bravo.txt']);
$results = json_decode($file_contents['system_changes.json'], TRUE);
$expected_pre_apply_results = [
'return value of existing global function' => 'pre-update-value',
'new global function exists' => 'not exists',
];
$this->assertSame($expected_pre_apply_results, $results['pre']);
$expected_post_apply_results = [
// Existing functions will still use the pre-update version.
'return value of existing global function' => 'pre-update-value',
// New functions that were added in .module files will not be available.
'new global function exists' => 'not exists',
];
$this->assertSame($expected_post_apply_results, $results['post']);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment