Skip to content

Commit f1d494e

Browse files
authored
tests: update for psr14 with cron looking at config page (simplesamlphp#2568)
* tests: update for psr14 with cron looking at config page * put those around the right way * I found that the key in $response->data['summary'] is different In testRunCorrectKey() doing var_dump($response->data['summary']); ``` array(1) { 'cron info' => string(57) "Cron did run tag [daily] at Fri, 28 Nov..." } ``` It seems before that the keys were just numeric [1] so maybe we should change runTag to do this (or duplicate the keys with numeric) to be non breaking? [1] https://github.com/simplesamlphp/simplesamlphp/blob/87bd56f96bf570689cd27f8de6dcad81a93ed8f5/modules/cron/hooks/hook_cron.php#L21 * I think this might fix this CI failure * make cron a core module to see if the 2 tests pass in CI * make the module listenerprovider use the core modules list by def * we do not want to make that temporary change perm * test * see it early * test2 * test3 * test4 * allow instance to be remade * cleanup
1 parent 23c589b commit f1d494e

5 files changed

Lines changed: 53 additions & 10 deletions

File tree

src/SimpleSAML/Configuration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ public static function getInstance(string $instancename = 'simplesaml'): Configu
365365
if (array_key_exists($instancename, self::$instance)) {
366366
return self::$instance[$instancename];
367367
}
368-
369368
if ($instancename === 'simplesaml') {
370369
try {
371370
return self::getConfig();

src/SimpleSAML/Event/Dispatcher/ModuleEventDispatcherFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ public static function getInstance(): EventDispatcher
1919

2020
return self::$instance;
2121
}
22-
}
22+
23+
public static function testingRemakeInstance(): EventDispatcher
24+
{
25+
$provider = new ModuleListenerProvider();
26+
self::$instance = new EventDispatcher($provider);
27+
return self::$instance;
28+
}
29+
30+
}

src/SimpleSAML/Event/Provider/ModuleListenerProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Psr\EventDispatcher\ListenerProviderInterface;
88
use SimpleSAML\Configuration;
9+
use SimpleSAML\Module;
910

1011
class ModuleListenerProvider implements ListenerProviderInterface
1112
{
@@ -15,7 +16,7 @@ class ModuleListenerProvider implements ListenerProviderInterface
1516
public function __construct()
1617
{
1718
$configuration = Configuration::getInstance();
18-
$enabledModules = $configuration->getArray('module.enable', []);
19+
$enabledModules = $configuration->getOptionalArray('module.enable', Module::$core_modules);
1920
$this->discoverListeners($enabledModules);
2021
}
2122

@@ -127,4 +128,4 @@ private function getClassNameFromFile(string $file, string $moduleName): ?string
127128
$className = "SimpleSAML\\Module\\{$moduleName}\\Event\\Listener\\{$basename}";
128129
return $className;
129130
}
130-
}
131+
}

tests/modules/cron/src/Controller/CronTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use SimpleSAML\Session;
1414
use SimpleSAML\Utils;
1515
use SimpleSAML\XHTML\Template;
16+
use SimpleSAML\Event\Dispatcher\ModuleEventDispatcherFactory;
1617

1718
/**
1819
* Set of tests for the controllers in the "cron" module.
@@ -65,13 +66,18 @@ public function requireAdmin(): void
6566
'key' => 'verysecret',
6667
'allowed_tags' => ['daily'],
6768
'sendemail' => false,
69+
'module.enable' => ['cron' => true],
70+
'debug_message' => true,
6871
],
6972
'[ARRAY]',
7073
'simplesaml',
7174
),
7275
'module_cron.php',
7376
'simplesaml',
7477
);
78+
79+
ModuleEventDispatcherFactory::testingRemakeInstance();
80+
7581
}
7682

7783

@@ -101,6 +107,7 @@ public function testInfo(): void
101107
*/
102108
public function testRunCorrectKey(): void
103109
{
110+
104111
$_SERVER['REQUEST_URI'] = '/module.php/cron/run/daily/verysecret';
105112

106113
$c = new Controller\Cron($this->config, $this->session);
@@ -112,7 +119,8 @@ public function testRunCorrectKey(): void
112119
$this->assertFalse($response->data['mail_required']);
113120
$this->assertArrayHasKey('time', $response->data);
114121
$this->assertCount(1, $response->data['summary']);
115-
$this->assertEquals('Cron did run tag [daily] at ' . $response->data['time'], $response->data['summary'][0]);
122+
123+
$this->assertEquals('Cron did run tag [daily] at ' . $response->data['time'], $response->data['summary']['cron info']);
116124
}
117125

118126

tests/src/SimpleSAML/ModuleTest.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
use SimpleSAML\Module;
1212
use Symfony\Component\Filesystem\Path;
1313

14+
use SimpleSAML\Event\Dispatcher\ModuleEventDispatcherFactory;
15+
use SimpleSAML\Module\admin\Event\ConfigPageEvent;
16+
use SimpleSAML\XHTML\Template;
17+
use SimpleSAML\Locale\Translate;
18+
1419
/**
1520
*/
1621
#[CoversClass(Module::class)]
@@ -121,11 +126,33 @@ public function testResolveClass(): void
121126
*/
122127
public function testGetModuleHooks(): void
123128
{
124-
$hooks = Module::getModuleHooks('cron');
125-
$this->assertArrayHasKey('configpage', $hooks);
126-
$this->assertEquals('cron_hook_configpage', $hooks['configpage']['func']);
127-
$expectedFile = Path::canonicalize(dirname(__DIR__, 3) . '/modules/cron/hooks/hook_configpage.php');
128-
$this->assertEquals($expectedFile, $hooks['configpage']['file']);
129+
$c = Configuration::loadFromArray(
130+
[
131+
'assets' => [ 'salt' => '1234567890'],
132+
'module.enable' => ['saml' => true, 'admin' => true, 'cron' => true,],
133+
], '', 'simplesaml');
134+
Configuration::setPreLoadedConfig($c);
135+
$t = new Template($c, 'admin:config.twig');
136+
$t->data = [
137+
'links' => [
138+
[
139+
'href' => Module::getModuleURL('admin/diagnostics'),
140+
'text' => Translate::noop('Diagnostics on hostname, port and protocol'),
141+
],
142+
[
143+
'href' => Module::getModuleURL('admin/phpinfo'),
144+
'text' => Translate::noop('Information on your PHP installation'),
145+
],
146+
],
147+
];
148+
149+
$eventDispatcher = ModuleEventDispatcherFactory::getInstance();
150+
$event = $eventDispatcher->dispatch(new ConfigPageEvent($t));
151+
$t = $event->getTemplate();
152+
$this->assertEquals(
153+
\SimpleSAML\Locale\Translate::noop('Cron module information page'),
154+
$t->data['links'][2]['text']);
155+
129156
}
130157

131158

0 commit comments

Comments
 (0)