From dda0d4f87d1aa9b3e5347383763f33ac45b570e6 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Thu, 28 Nov 2024 08:26:35 +0100 Subject: [PATCH 1/3] Migrate mail tests to PHPUnit --- .github/actions/test_tests-imap.sh | 17 +- .../imap/MailCollectorTest.php | 391 +++++++++--------- tests/README.md | 4 +- .../plugins/tester/inc/fakeprotocol.class.php | 49 +++ .../plugins/tester/inc/fakestorage.class.php | 45 ++ 5 files changed, 286 insertions(+), 220 deletions(-) rename tests/imap/MailCollector.php => phpunit/imap/MailCollectorTest.php (81%) create mode 100644 tests/fixtures/plugins/tester/inc/fakeprotocol.class.php create mode 100644 tests/fixtures/plugins/tester/inc/fakestorage.class.php diff --git a/.github/actions/test_tests-imap.sh b/.github/actions/test_tests-imap.sh index 154b7aa923e..23de9f72b8c 100755 --- a/.github/actions/test_tests-imap.sh +++ b/.github/actions/test_tests-imap.sh @@ -1,23 +1,14 @@ #!/bin/bash set -e -u -x -o pipefail -ATOUM_ADDITIONNAL_OPTIONS="" +PHPUNIT_ADDITIONNAL_OPTIONS="" if [[ "$CODE_COVERAGE" = true ]]; then export COVERAGE_DIR="coverage-imap" + PHPUNIT_ADDITIONNAL_OPTIONS="--coverage-filter src --coverage-clover phpunit/$COVERAGE_DIR/clover.xml" else - ATOUM_ADDITIONNAL_OPTIONS="--no-code-coverage"; + PHPUNIT_ADDITIONNAL_OPTIONS="--no-coverage"; fi -vendor/bin/atoum \ - -p 'php -d memory_limit=512M' \ - --debug \ - --force-terminal \ - --use-dot-report \ - --bootstrap-file tests/bootstrap.php \ - --fail-if-void-methods \ - --fail-if-skipped-methods \ - $ATOUM_ADDITIONNAL_OPTIONS \ - --max-children-number 1 \ - -d tests/imap +vendor/bin/phpunit $PHPUNIT_ADDITIONNAL_OPTIONS phpunit/imap unset COVERAGE_DIR diff --git a/tests/imap/MailCollector.php b/phpunit/imap/MailCollectorTest.php similarity index 81% rename from tests/imap/MailCollector.php rename to phpunit/imap/MailCollectorTest.php index b207549a710..4a0daef1ee6 100644 --- a/tests/imap/MailCollector.php +++ b/phpunit/imap/MailCollectorTest.php @@ -42,49 +42,50 @@ use NotificationTarget; use NotificationTargetSoftwareLicense; use NotificationTargetTicket; +use PHPUnit\Framework\Attributes\DataProvider; use SoftwareLicense; use Ticket; use Psr\Log\LogLevel; -class MailCollector extends DbTestCase +class MailCollectorTest extends DbTestCase { private $collector; private $mailgate_id; public function testGetEmpty() { - $this - ->if($this->newTestedInstance) - ->then - ->array($this->testedInstance->fields) - ->isIdenticalTo([]) - ->boolean($this->testedInstance->getEmpty()) - ->array($this->testedInstance->fields) - ->isIdenticalTo([ - 'id' => '', - 'name' => '', - 'host' => '', - 'login' => '', - 'filesize_max' => '2097152', - 'is_active' => 1, - 'date_mod' => '', - 'comment' => '', - 'passwd' => '', - 'accepted' => '', - 'refused' => '', - 'errors' => '', - 'use_mail_date' => '', - 'date_creation' => '', - 'requester_field' => '', + $instance = new \MailCollector(); + $this->assertSame([], $instance->fields); + + $this->assertTrue($instance->getEmpty()); + $this->assertSame( + [ + 'id' => '', + 'name' => '', + 'host' => '', + 'login' => '', + 'filesize_max' => '2097152', + 'is_active' => 1, + 'date_mod' => '', + 'comment' => '', + 'passwd' => '', + 'accepted' => '', + 'refused' => '', + 'errors' => '', + 'use_mail_date' => '', + 'date_creation' => '', + 'requester_field' => '', 'add_to_to_observer' => '', - 'add_cc_to_observer' => '', - 'collect_only_unread' => '', + 'add_cc_to_observer' => '', + 'collect_only_unread' => '', 'create_user_from_email' => '', - 'last_collect_date' => '', - ]); + 'last_collect_date' => '', + ], + $instance->fields + ); } - protected function subjectProvider() + public static function subjectProvider() { return [ [ @@ -103,106 +104,109 @@ protected function subjectProvider() ]; } - /** - * @dataProvider subjectProvider - */ + #[DataProvider('subjectProvider')] public function testCleanSubject($raw, $expected) { - $this - ->if($this->newTestedInstance) - ->then - ->string($this->testedInstance->cleanSubject($raw)) - ->isIdenticalTo($expected); + $instance = new \MailCollector(); + $this->assertSame($expected, $instance->cleanSubject($raw)); } public function testPrepareInput() { $_SESSION['glpicronuserrunning'] = 'cron_phpunit'; - $this->newTestedInstance(); + $instance = new \MailCollector(); $oinput = [ 'passwd' => 'Ph34r', 'is_active' => true ]; - $prepared = $this->testedInstance->prepareInput($oinput, 'add'); - $this->array($prepared) - ->boolean['is_active']->isTrue() - ->string['passwd']->isNotEqualTo($oinput['passwd']); + $prepared = $instance->prepareInput($oinput, 'add'); + $this->assertIsArray($prepared); + $this->assertTrue($prepared['is_active']); + $this->assertNotEquals($oinput['passwd'], $prepared['passwd']); - //empty password means no password. + //empty password means no password. $oinput = [ 'passwd' => '', 'is_active' => true ]; - $this->array($this->testedInstance->prepareInput($oinput, 'add')) - ->isIdenticalTo(['is_active' => true]); + $this->assertSame( + ['is_active' => true], + $instance->prepareInput($oinput, 'add') + ); - //manage host + //manage host $oinput = [ 'mail_server' => 'mail.example.com' ]; - $this->array($this->testedInstance->prepareInput($oinput, 'add')) - ->isIdenticalTo(['mail_server' => 'mail.example.com', 'host' => '{mail.example.com}']); + $this->assertSame( + ['mail_server' => 'mail.example.com', 'host' => '{mail.example.com}'], + $instance->prepareInput($oinput, 'add') + ); - //manage host + //manage host $oinput = [ 'mail_server' => 'mail.example.com', 'server_port' => 143, 'server_mailbox' => 'bugs' ]; - $this->array($this->testedInstance->prepareInput($oinput, 'add')) - ->isIdenticalTo([ - 'mail_server' => 'mail.example.com', - 'server_port' => 143, - 'server_mailbox' => 'bugs', - 'host' => '{mail.example.com:143}bugs' - ]); + $this->assertSame( + [ + 'mail_server' => 'mail.example.com', + 'server_port' => 143, + 'server_mailbox' => 'bugs', + 'host' => '{mail.example.com:143}bugs' + ], + $instance->prepareInput($oinput, 'add') + ); $oinput = [ 'passwd' => 'Ph34r', '_blank_passwd' => true ]; - $this->array($this->testedInstance->prepareInputForUpdate($oinput)) - ->isIdenticalTo(['passwd' => '', '_blank_passwd' => true]); + $this->assertSame( + ['passwd' => '', '_blank_passwd' => true], + $instance->prepareInputForUpdate($oinput) + ); } public function testCounts() { $_SESSION['glpicronuserrunning'] = 'cron_phpunit'; - $this->newTestedInstance(); + $instance = new \MailCollector(); - $this->integer($this->testedInstance->countActiveCollectors())->isIdenticalTo(0); - $this->integer($this->testedInstance->countCollectors(true))->isIdenticalTo(0); - $this->integer($this->testedInstance->countCollectors())->isIdenticalTo(0); + $this->assertEquals(0, $instance->countActiveCollectors()); + $this->assertEquals(0, $instance->countCollectors(true)); + $this->assertEquals(0, $instance->countCollectors()); - //Add an active collector - $nid = (int)$this->testedInstance->add([ + //Add an active collector + $nid = (int)$instance->add([ 'name' => 'Maille name', 'is_active' => true ]); - $this->integer($nid)->isGreaterThan(0); + $this->assertGreaterThan(0, $nid); - $this->integer($this->testedInstance->countActiveCollectors())->isIdenticalTo(1); - $this->integer($this->testedInstance->countCollectors(true))->isIdenticalTo(1); - $this->integer($this->testedInstance->countCollectors())->isIdenticalTo(1); + $this->assertEquals(1, $instance->countActiveCollectors()); + $this->assertEquals(1, $instance->countCollectors(true)); + $this->assertEquals(1, $instance->countCollectors()); - $this->boolean( - $this->testedInstance->update([ - 'id' => $this->testedInstance->fields['id'], + $this->assertTrue( + $instance->update([ + 'id' => $instance->fields['id'], 'is_active' => false ]) - )->isTrue(); + ); - $this->integer($this->testedInstance->countActiveCollectors())->isIdenticalTo(0); - $this->integer($this->testedInstance->countCollectors(true))->isIdenticalTo(0); - $this->integer($this->testedInstance->countCollectors())->isIdenticalTo(1); + $this->assertEquals(0, $instance->countActiveCollectors()); + $this->assertEquals(0, $instance->countCollectors(true)); + $this->assertEquals(1, $instance->countCollectors()); } - protected function messageIdHeaderProvider() + public static function messageIdHeaderProvider() { $root_ent_id = getItemByTypeName('Entity', '_test_root_entity', true); @@ -311,12 +315,10 @@ protected function messageIdHeaderProvider() ]; } - /** - * @dataProvider messageIdHeaderProvider - */ + #[DataProvider('messageIdHeaderProvider')] public function testIsMessageSentByGlpi(array $headers, bool $expected) { - $this->newTestedInstance(); + $instance = new \MailCollector(); $message = new Message( [ @@ -325,10 +327,10 @@ public function testIsMessageSentByGlpi(array $headers, bool $expected) ] ); - $this->boolean($this->testedInstance->isMessageSentByGlpi($message))->isEqualTo($expected); + $this->assertEquals($expected, $instance->isMessageSentByGlpi($message)); } - protected function itemReferenceHeaderProvider() + public static function itemReferenceHeaderProvider() { $root_ent_id = getItemByTypeName('Entity', '_test_root_entity', true); @@ -529,16 +531,14 @@ protected function itemReferenceHeaderProvider() ]; } - /** - * @dataProvider itemReferenceHeaderProvider - */ + #[DataProvider('itemReferenceHeaderProvider')] public function testGetItemFromHeader( array $headers, ?string $expected_itemtype, ?int $expected_items_id, bool $accepted ) { - $this->newTestedInstance(); + $instance = new \MailCollector(); $message = new Message( [ @@ -547,26 +547,24 @@ public function testGetItemFromHeader( ] ); - $item = $this->testedInstance->getItemFromHeaders($message); + $item = $instance->getItemFromHeaders($message); if ($expected_itemtype === null) { - $this->variable($item)->isNull(); + $this->assertNull($item); } else { - $this->object($item)->isInstanceOf($expected_itemtype); - $this->integer($item->getId())->isEqualTo($expected_items_id); + $this->assertInstanceOf($expected_itemtype, $item); + $this->assertEquals($expected_items_id, $item->getId()); } } - /** - * @dataProvider itemReferenceHeaderProvider - */ + #[DataProvider('itemReferenceHeaderProvider')] public function testIsResponseToMessageSentByAnotherGlpi( array $headers, ?string $expected_itemtype, ?int $expected_items_id, bool $accepted ) { - $this->newTestedInstance(); + $instance = new \MailCollector(); $message = new Message( [ @@ -575,14 +573,13 @@ public function testIsResponseToMessageSentByAnotherGlpi( ] ); - $this->boolean($this->testedInstance->isResponseToMessageSentByAnotherGlpi($message))->isEqualTo(!$accepted); + $this->assertEquals(!$accepted, $instance->isResponseToMessageSentByAnotherGlpi($message)); } private function doConnect() { if (null === $this->collector) { - $this->newTestedInstance(); - $collector = $this->testedInstance; + $collector = new \MailCollector(); $this->collector = $collector; } else { $collector = $this->collector; @@ -604,12 +601,12 @@ private function doConnect() 'requester_field' => \MailCollector::REQUESTER_FIELD_REPLY_TO, ]); - $this->integer($this->mailgate_id)->isGreaterThan(0); + $this->assertGreaterThan(0, $this->mailgate_id); - $this->boolean($collector->getFromDB($this->mailgate_id))->isTrue(); - $this->string($collector->fields['host'])->isIdenticalTo('{dovecot:143/imap/novalidate-cert}'); + $this->assertTrue($collector->getFromDB($this->mailgate_id)); + $this->assertSame('{dovecot:143/imap/novalidate-cert}', $collector->fields['host']); $collector->connect(); - $this->variable($collector->fields['errors'])->isEqualTo(0); + $this->assertEquals(0, $collector->fields['errors']); } public function testCollect() @@ -617,38 +614,41 @@ public function testCollect() global $DB; $_SESSION['glpicronuserrunning'] = 'cron_phpunit'; - // Force notification_uuid + // Force notification_uuid Config::setConfigurationValues('core', ['notification_uuid' => 't3StN0t1f1c4tiOnUUID']); - //assign email to user + //assign email to user $nuid = getItemByTypeName('User', 'normal', true); $uemail = new \UserEmail(); - $this->integer( + $this->assertGreaterThan( + 0, (int)$uemail->add([ 'users_id' => $nuid, 'is_default' => 1, 'email' => 'normal@glpi-project.org' ]) - )->isGreaterThan(0); + ); $tuid = getItemByTypeName('User', 'tech', true); - $this->integer( + $this->assertGreaterThan( + 0, (int)$uemail->add([ 'users_id' => $tuid, 'is_default' => 1, 'email' => 'tech@glpi-project.org' ]) - )->isGreaterThan(0); + ); - // Hack to allow documents named "1234567890", "1234567890_2", ... (cf 28-multiple-attachments-no-extension.eml) + // Hack to allow documents named "1234567890", "1234567890_2", ... (cf 28-multiple-attachments-no-extension.eml) $doctype = new \DocumentType(); - $this->integer( + $this->assertGreaterThan( + 0, $doctype->add([ 'name' => 'Type test', 'ext' => '/^1234567890(_\d+)?$/' ]) - )->isGreaterThan(0); + ); - // Collect all mails + // Collect all mails $this->doConnect(); $this->collector->maxfetch_emails = 1000; // Be sure to fetch all mails from test suite @@ -659,16 +659,11 @@ public function testCollect() 'Header with Name date or date not found' => LogLevel::CRITICAL, ]; - $msg = null; - $this->when( - function () use (&$msg) { - $msg = $this->collector->collect($this->mailgate_id); - } - ) - ->error() - ->withType(E_USER_WARNING) - ->withMessage('Invalid header "X-Invalid-Encoding"') - ->exists(); + $msg = $this->collector->collect($this->mailgate_id); + $this->hasPhpLogRecordThatContains( + 'Invalid header "X-Invalid-Encoding"', + LogLevel::WARNING + ); // Check error log and clean it (to prevent test failure, see GLPITestCase::afterTestMethod()). foreach ($expected_logged_errors as $error_message => $error_level) { @@ -681,7 +676,7 @@ function () use (&$msg) { $expected_blacklist_count = 3; $expected_expected_already_seen = 0; - $this->variable($msg)->isIdenticalTo( + $this->assertSame( sprintf( 'Number of messages: available=%1$s, already imported=%2$d, retrieved=%3$s, refused=%4$s, errors=%5$s, blacklisted=%6$s', $total_count, @@ -690,10 +685,11 @@ function () use (&$msg) { $expected_refused_count, $expected_error_count, $expected_blacklist_count - ) + ), + $msg ); - // Check not imported emails + // Check not imported emails $not_imported_specs = [ [ 'subject' => 'Have a problem, can you help me?', @@ -715,7 +711,7 @@ function () use (&$msg) { ] ]; $iterator = $DB->request(['FROM' => \NotImportedEmail::getTable()]); - $this->integer(count($iterator))->isIdenticalTo(count($not_imported_specs)); + $this->assertEquals(count($not_imported_specs), count($iterator)); $not_imported_values = []; foreach ($iterator as $data) { @@ -725,13 +721,13 @@ function () use (&$msg) { 'to' => $data['to'], 'reason' => $data['reason'], ]; - $this->integer($data['mailcollectors_id'])->isIdenticalTo($this->mailgate_id); + $this->assertSame($this->mailgate_id, $data['mailcollectors_id']); } - $this->array($not_imported_values)->isIdenticalTo($not_imported_specs); + $this->assertSame($not_imported_specs, $not_imported_values); - // Check created tickets and their actors + // Check created tickets and their actors $actors_specs = [ - // Mails having "tech" user as requester + // Mails having "tech" user as requester [ 'users_id' => $tuid, 'actor_type' => \CommonITILActor::REQUESTER, @@ -743,12 +739,12 @@ function () use (&$msg) { 'Ticket with multiple to', ] ], - // Mails having "normal" user as requester + // Mails having "normal" user as requester [ 'users_id' => $nuid, 'actor_type' => \CommonITILActor::REQUESTER, 'tickets_names' => [ - 'Test import mail avec emoticons 😃 unicode', + 'Test import mail avec emoticons � unicode', 'Test images', 'Test\'ed issue', 'Test Email from Outlook', @@ -795,9 +791,9 @@ function () use (&$msg) { ], ]; - // Tickets on which content should be checked (key is ticket name) + // Tickets on which content should be checked (key is ticket name) $tickets_contents = [ - // Plain text on mono-part email + // Plain text on mono-part email 'PHP fatal error' => << <<<HTML <p>This message have reply to header, requester should be get from this header.</p> HTML, @@ -926,27 +922,28 @@ function () use (&$msg) { ] ]); - $this->integer(count($iterator))->isIdenticalTo(count($actor_specs['tickets_names'])); + $this->assertSame(count($actor_specs['tickets_names']), count($iterator)); $names = []; foreach ($iterator as $data) { $name = $data['name']; if (array_key_exists($name, $tickets_contents)) { - $this->string($data['content'])->isEqualTo($tickets_contents[$name]); + $this->assertEquals($tickets_contents[$name], $data['content']); } - $this->string($data['content'])->notContains('cid:'); // check that image were correctly imported + $this->assertStringNotContainsString('cid:', $data['content']); // check that images were correctly imported $names[] = $name; } sort($names); sort($actor_specs['tickets_names']); - $this->array($names)->isIdenticalTo($actor_specs['tickets_names']); + $this->assertSame($actor_specs['tickets_names'], $names); + } - // Check creation of expected documents + // Check creation of expected documents $expected_docs = [ '00-logoteclib.png' => 'image/png', '01-screenshot-2018-4-12-observatoire-france-tres-haut-debit.png' => 'image/png', @@ -992,11 +989,11 @@ function () use (&$msg) { } ksort($filenames); ksort($expected_docs); - $this->array($filenames)->isIdenticalTo($expected_docs); + $this->assertSame($expected_docs, $filenames); - $this->integer(count($iterator))->isIdenticalTo(count($expected_docs)); + $this->assertSame(count($expected_docs), count($iterator)); - // Check creation of expected followups + // Check creation of expected followups $expected_followups = [ [ 'items_id' => 100, @@ -1072,12 +1069,15 @@ function () use (&$msg) { ]; foreach ($expected_followups as $expected_followup) { - $this->integer(countElementsInTable(ITILFollowup::getTable(), $expected_followup)) - ->isEqualTo(1, sprintf("Followup not found:\n> %s", $expected_followup['content'])); + $this->assertEquals( + 1, + countElementsInTable(ITILFollowup::getTable(), $expected_followup), + sprintf("Followup not found:\n> %s", $expected_followup['content']) + ); } } - protected function mailServerProtocolsProvider() + public static function mailServerProtocolsProvider() { return [ [ @@ -1119,9 +1119,7 @@ protected function mailServerProtocolsProvider() ]; } - /** - * @dataProvider mailServerProtocolsProvider - */ + #[DataProvider('mailServerProtocolsProvider')] public function testGetMailServerProtocols( string $cnx_string, string $expected_type, @@ -1130,12 +1128,12 @@ public function testGetMailServerProtocols( ) { $type = \Toolbox::parseMailServerConnectString($cnx_string)['type']; - $this->string($type)->isEqualTo($expected_type); + $this->assertEquals($expected_type, $type); if ($expected_protocol !== null) { - $this->object(\Toolbox::getMailServerProtocolInstance($type))->isInstanceOf($expected_protocol); + $this->assertInstanceOf($expected_protocol, \Toolbox::getMailServerProtocolInstance($type)); } else { - $this->variable(\Toolbox::getMailServerProtocolInstance($type))->isNull(); + $this->assertNull(\Toolbox::getMailServerProtocolInstance($type)); } $params = [ @@ -1144,31 +1142,17 @@ public function testGetMailServerProtocols( 'password' => 'applesauce', ]; if ($expected_storage !== null) { - $this->object(\Toolbox::getMailServerStorageInstance($type, $params))->isInstanceOf($expected_storage); + $this->assertInstanceOf($expected_storage, \Toolbox::getMailServerStorageInstance($type, $params)); } else { - $this->variable(\Toolbox::getMailServerStorageInstance($type, $params))->isNull(); + $this->assertNull(\Toolbox::getMailServerStorageInstance($type, $params)); } } - protected function mailServerProtocolsHookProvider() + public static function mailServerProtocolsHookProvider() { - // Create valid classes - eval(<<<CLASS -class PluginTesterFakeProtocol implements Glpi\Mail\Protocol\ProtocolInterface { - public function setNoValidateCert(bool \$novalidatecert) {} - public function connect(\$host, \$port = null, \$ssl = false) {} - public function login(\$user, \$password) {} -} -class PluginTesterFakeStorage extends Laminas\Mail\Storage\Imap { - public function __construct(\$params) {} - public function close() {} -} -CLASS - ); - return [ - // Check that invalid hook result does not alter core protocols specs + // Check that invalid hook result does not alter core protocols specs [ 'hook_result' => 'invalid result', 'type' => 'imap', @@ -1176,7 +1160,7 @@ public function close() {} 'expected_protocol' => 'Laminas\Mail\Protocol\Imap', 'expected_storage' => 'Laminas\Mail\Storage\Imap', ], - // Check that hook cannot alter core protocols specs + // Check that hook cannot alter core protocols specs [ 'hook_result' => [ 'imap' => [ @@ -1190,7 +1174,7 @@ public function close() {} 'expected_protocol' => 'Laminas\Mail\Protocol\Imap', 'expected_storage' => 'Laminas\Mail\Storage\Imap', ], - // Check that hook cannot alter core protocols specs + // Check that hook cannot alter core protocols specs [ 'hook_result' => [ 'pop' => [ @@ -1204,7 +1188,7 @@ public function close() {} 'expected_protocol' => 'Laminas\Mail\Protocol\Pop3', 'expected_storage' => 'Laminas\Mail\Storage\Pop3', ], - // Check that class must exists + // Check that class must exist [ 'hook_result' => [ 'custom-protocol' => [ @@ -1218,7 +1202,7 @@ public function close() {} 'expected_protocol' => null, 'expected_storage' => null, ], - // Check that class must implements expected functions + // Check that class must implement expected functions [ 'hook_result' => [ 'custom-protocol' => [ @@ -1232,21 +1216,21 @@ public function close() {} 'expected_protocol' => null, 'expected_storage' => null, ], - // Check valid case using class names + // Check valid case using class names [ 'hook_result' => [ 'custom-protocol' => [ 'label' => 'Custom email protocol', - 'protocol' => 'PluginTesterFakeProtocol', - 'storage' => 'PluginTesterFakeStorage', + 'protocol' => \PluginTesterFakeProtocol::class, + 'storage' => \PluginTesterFakeStorage::class, ], ], 'type' => 'custom-protocol', 'expected_warning' => null, - 'expected_protocol' => 'PluginTesterFakeProtocol', - 'expected_storage' => 'PluginTesterFakeStorage', + 'expected_protocol' => \PluginTesterFakeProtocol::class, + 'expected_storage' => \PluginTesterFakeStorage::class, ], - // Check valid case using callback + // Check valid case using callback [ 'hook_result' => [ 'custom-protocol' => [ @@ -1261,15 +1245,13 @@ public function close() {} ], 'type' => 'custom-protocol', 'expected_warning' => null, - 'expected_protocol' => 'PluginTesterFakeProtocol', - 'expected_storage' => 'PluginTesterFakeStorage', + 'expected_protocol' => \PluginTesterFakeProtocol::class, + 'expected_storage' => \PluginTesterFakeStorage::class, ], ]; } - /** - * @dataProvider mailServerProtocolsHookProvider - */ + #[DataProvider('mailServerProtocolsHookProvider')] public function testGetAdditionnalMailServerProtocols( $hook_result, string $type, @@ -1279,28 +1261,29 @@ public function testGetAdditionnalMailServerProtocols( ) { global $PLUGIN_HOOKS; + (new \Plugin())->init(true); // The `tester` plugin must be considered as loaded/active. + $hooks_backup = $PLUGIN_HOOKS; $PLUGIN_HOOKS['mail_server_protocols']['tester'] = function () use ($hook_result) { return $hook_result; }; - // Get protocol - $protocol = null; + // Get protocol + $protocol = null; $getProtocol = function () use ($type, &$protocol) { $protocol = \Toolbox::getMailServerProtocolInstance($type); }; + + $getProtocol(); if ($expected_warning !== null) { - $this->when($getProtocol) - ->error() - ->withType(E_USER_WARNING) - ->withMessage($expected_warning) - ->exists(); - } else { - $getProtocol(); + $this->hasPhpLogRecordThatContains( + $expected_warning, + LogLevel::WARNING + ); } - // Get storage + // Get storage $storage = null; $getStorage = function () use ($type, &$storage) { $params = [ @@ -1310,28 +1293,26 @@ public function testGetAdditionnalMailServerProtocols( ]; $storage = \Toolbox::getMailServerStorageInstance($type, $params); }; + $getStorage(); if ($expected_warning !== null) { - $this->when($getStorage) - ->error() - ->withType(E_USER_WARNING) - ->withMessage($expected_warning) - ->exists(); - } else { - $getStorage(); + $this->hasPhpLogRecordThatContains( + $expected_warning, + LogLevel::WARNING + ); } $PLUGIN_HOOKS = $hooks_backup; if ($expected_protocol !== null) { - $this->object($protocol)->isInstanceOf($expected_protocol); + $this->assertInstanceOf($expected_protocol, $protocol); } else { - $this->variable($protocol)->isNull(); + $this->assertNull($protocol); } if ($expected_storage !== null) { - $this->object($storage)->isInstanceOf($expected_storage); + $this->assertInstanceOf($expected_storage, $storage); } else { - $this->variable($storage)->isNull(); + $this->assertNull($storage); } } } diff --git a/tests/README.md b/tests/README.md index 6e887aca415..ff3e87cde6d 100644 --- a/tests/README.md +++ b/tests/README.md @@ -49,8 +49,8 @@ Running the test suite on developpement machine ----------------------------------------------- There are multiple directories for tests: -- `tests/functional` for unit and functional tests; -- `tests/imap` for Mail collector tests; +- `tests/functional` and `phpunit/functional` for unit and functional tests; +- `phpunit/imap` for Mail collector tests; - `tests/LDAP` for LDAP connection tests; - `tests/web` for API tests. diff --git a/tests/fixtures/plugins/tester/inc/fakeprotocol.class.php b/tests/fixtures/plugins/tester/inc/fakeprotocol.class.php new file mode 100644 index 00000000000..9681b83876f --- /dev/null +++ b/tests/fixtures/plugins/tester/inc/fakeprotocol.class.php @@ -0,0 +1,49 @@ +<?php + +/** + * --------------------------------------------------------------------- + * + * GLPI - Gestionnaire Libre de Parc Informatique + * + * http://glpi-project.org + * + * @copyright 2015-2024 Teclib' and contributors. + * @copyright 2003-2014 by the INDEPNET Development Team. + * @licence https://www.gnu.org/licenses/gpl-3.0.html + * + * --------------------------------------------------------------------- + * + * LICENSE + * + * This file is part of GLPI. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + * --------------------------------------------------------------------- + */ + +class PluginTesterFakeProtocol implements Glpi\Mail\Protocol\ProtocolInterface +{ + public function setNoValidateCert(bool $novalidatecert) + { + } + + public function connect($host, $port = null, $ssl = false) + { + } + + public function login($user, $password) + { + } +} diff --git a/tests/fixtures/plugins/tester/inc/fakestorage.class.php b/tests/fixtures/plugins/tester/inc/fakestorage.class.php new file mode 100644 index 00000000000..4c97e1433e2 --- /dev/null +++ b/tests/fixtures/plugins/tester/inc/fakestorage.class.php @@ -0,0 +1,45 @@ +<?php + +/** + * --------------------------------------------------------------------- + * + * GLPI - Gestionnaire Libre de Parc Informatique + * + * http://glpi-project.org + * + * @copyright 2015-2024 Teclib' and contributors. + * @copyright 2003-2014 by the INDEPNET Development Team. + * @licence https://www.gnu.org/licenses/gpl-3.0.html + * + * --------------------------------------------------------------------- + * + * LICENSE + * + * This file is part of GLPI. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + * --------------------------------------------------------------------- + */ + +class PluginTesterFakeStorage extends Laminas\Mail\Storage\Imap +{ + public function __construct($params) + { + } + + public function close() + { + } +} From c910909a13241d078decb6cd16033abbd19570a1 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski <johan@x-tnd.be> Date: Thu, 28 Nov 2024 09:05:54 +0100 Subject: [PATCH 2/3] Fix CS --- phpunit/imap/MailCollectorTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/phpunit/imap/MailCollectorTest.php b/phpunit/imap/MailCollectorTest.php index 4a0daef1ee6..86e53456cfa 100644 --- a/phpunit/imap/MailCollectorTest.php +++ b/phpunit/imap/MailCollectorTest.php @@ -75,10 +75,10 @@ public function testGetEmpty() 'use_mail_date' => '', 'date_creation' => '', 'requester_field' => '', - 'add_to_to_observer' => '', + 'add_to_to_observer' => '', 'add_cc_to_observer' => '', 'collect_only_unread' => '', - 'create_user_from_email' => '', + 'create_user_from_email' => '', 'last_collect_date' => '', ], $instance->fields @@ -940,7 +940,6 @@ public function testCollect() sort($names); sort($actor_specs['tickets_names']); $this->assertSame($actor_specs['tickets_names'], $names); - } // Check creation of expected documents From 65192ba75628af2a6932d264ba110a0ffe4d6ab0 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski <johan@x-tnd.be> Date: Thu, 28 Nov 2024 10:40:19 +0100 Subject: [PATCH 3/3] FIx encoding --- phpunit/imap/MailCollectorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/imap/MailCollectorTest.php b/phpunit/imap/MailCollectorTest.php index 86e53456cfa..303aabf5084 100644 --- a/phpunit/imap/MailCollectorTest.php +++ b/phpunit/imap/MailCollectorTest.php @@ -744,7 +744,7 @@ public function testCollect() 'users_id' => $nuid, 'actor_type' => \CommonITILActor::REQUESTER, 'tickets_names' => [ - 'Test import mail avec emoticons � unicode', + 'Test import mail avec emoticons 😃 unicode', 'Test images', 'Test\'ed issue', 'Test Email from Outlook',