From 83649aa55018aa766338d6dbd0985a67fdf93dca Mon Sep 17 00:00:00 2001 From: Matthew Hilton Date: Fri, 1 Sep 2023 08:25:07 +1000 Subject: [PATCH] [#443] Update factor email tests --- factor/email/tests/factor_test.php | 36 ++++++++++-------------------- 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/factor/email/tests/factor_test.php b/factor/email/tests/factor_test.php index faeb347d..e61ace97 100644 --- a/factor/email/tests/factor_test.php +++ b/factor/email/tests/factor_test.php @@ -35,29 +35,23 @@ public function generate_email_ip_address_location_provider(): array { return [ 'real ip v4' => [ // Note - this is the same IP address used by core_iplookup_geoplugin_testcase. - 'ip' => '50.0.184.0', - 'isunknown' => false + 'ip' => '50.0.184.0' ], 'real ip v6' => [ // Ipv6 is not supported by geoplugin, so it should be treated as unknown. - 'ip' => '2a01:8900:2:3:8c6c:c0db:3d33:9ce6', - 'isunknown' => true + 'ip' => '2a01:8900:2:3:8c6c:c0db:3d33:9ce6' ], 'empty ip' => [ - 'ip' => '', - 'isunknown' => true + 'ip' => '' ], 'malformed ip' => [ - 'ip' => '1.1.1.1.1.1.1.1.1.1', - 'isunknown' => true + 'ip' => '1.1.1.1.1.1.1.1.1.1' ], 'localhost' => [ - 'ip' => '0.0.0.0', - 'isunknown' => true + 'ip' => '0.0.0.0' ], 'malformed ip 2' => [ - 'ip' => 'aaaaaa', - 'isunknown' => true + 'ip' => 'aaaaaa' ] ]; } @@ -66,11 +60,10 @@ public function generate_email_ip_address_location_provider(): array { * Tests the rendererer generate_email function with regard to its * * @param string $ip IP address to test - * @param bool $expectedunknown if the IP given should return an email with the unknown message in it. * * @dataProvider generate_email_ip_address_location_provider */ - public function test_generate_email_ip_address_location(string $ip, bool $expectedunknown) { + public function test_generate_email_ip_address_location(string $ip) { global $DB, $PAGE; $this->resetAfterTest(true); @@ -97,15 +90,10 @@ public function test_generate_email_ip_address_location(string $ip, bool $expect // We ignore this in this unit test. $this->resetDebugging(); - $unknownstr = get_string('email:geoinfo:unknown', 'factor_email'); - - if ($expectedunknown) { - $this->assertStringContainsString($unknownstr, $email); - } else { - // Ideally we would test the email contains the correct geo info, - // but since the geo location of a real IP address can change - // we instead test that the unknown message is not there. - $this->assertStringNotContainsString($unknownstr, $email); - } + // Note it's difficult to know beforehand where a IP address will resolve to. + // So instead, we just check that it contains EITHER a location or an unknown message. + $containslocation = strpos($email, 'This request appears to have originated from approximately') != false; + $containsunknown = strpos($email, get_string('email:geoinfo:unknown', 'factor_email')) != false; + $this->assertTrue($containslocation || $containsunknown); } }