Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

factor_email Port improvements to tests from moodle branch #456

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions factor/email/tests/factor_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
];
}
Expand All @@ -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);

Expand All @@ -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->assertContains($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->assertNotContains($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);
}
}
Loading