Skip to content

Commit d919e1b

Browse files
committed
factor_email Port improvements to tests from moodle branch
1 parent a5877d5 commit d919e1b

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

factor/email/tests/factor_test.php

+12-24
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,23 @@ public function generate_email_ip_address_location_provider(): array {
3535
return [
3636
'real ip v4' => [
3737
// Note - this is the same IP address used by core_iplookup_geoplugin_testcase.
38-
'ip' => '50.0.184.0',
39-
'isunknown' => false
38+
'ip' => '50.0.184.0'
4039
],
4140
'real ip v6' => [
4241
// Ipv6 is not supported by geoplugin, so it should be treated as unknown.
43-
'ip' => '2a01:8900:2:3:8c6c:c0db:3d33:9ce6',
44-
'isunknown' => true
42+
'ip' => '2a01:8900:2:3:8c6c:c0db:3d33:9ce6'
4543
],
4644
'empty ip' => [
47-
'ip' => '',
48-
'isunknown' => true
45+
'ip' => ''
4946
],
5047
'malformed ip' => [
51-
'ip' => '1.1.1.1.1.1.1.1.1.1',
52-
'isunknown' => true
48+
'ip' => '1.1.1.1.1.1.1.1.1.1'
5349
],
5450
'localhost' => [
55-
'ip' => '0.0.0.0',
56-
'isunknown' => true
51+
'ip' => '0.0.0.0'
5752
],
5853
'malformed ip 2' => [
59-
'ip' => 'aaaaaa',
60-
'isunknown' => true
54+
'ip' => 'aaaaaa'
6155
]
6256
];
6357
}
@@ -66,11 +60,10 @@ public function generate_email_ip_address_location_provider(): array {
6660
* Tests the rendererer generate_email function with regard to its
6761
*
6862
* @param string $ip IP address to test
69-
* @param bool $expectedunknown if the IP given should return an email with the unknown message in it.
7063
*
7164
* @dataProvider generate_email_ip_address_location_provider
7265
*/
73-
public function test_generate_email_ip_address_location(string $ip, bool $expectedunknown) {
66+
public function test_generate_email_ip_address_location(string $ip) {
7467
global $DB, $PAGE;
7568
$this->resetAfterTest(true);
7669

@@ -97,15 +90,10 @@ public function test_generate_email_ip_address_location(string $ip, bool $expect
9790
// We ignore this in this unit test.
9891
$this->resetDebugging();
9992

100-
$unknownstr = get_string('email:geoinfo:unknown', 'factor_email');
101-
102-
if ($expectedunknown) {
103-
$this->assertContains($unknownstr, $email);
104-
} else {
105-
// Ideally we would test the email contains the correct geo info,
106-
// but since the geo location of a real IP address can change
107-
// we instead test that the unknown message is not there.
108-
$this->assertNotContains($unknownstr, $email);
109-
}
93+
// Note it's difficult to know beforehand where a IP address will resolve to.
94+
// So instead, we just check that it contains EITHER a location or an unknown message.
95+
$containslocation = strpos($email, 'This request appears to have originated from approximately') != false;
96+
$containsunknown = strpos($email, get_string('email:geoinfo:unknown', 'factor_email')) != false;
97+
$this->assertTrue($containslocation || $containsunknown);
11098
}
11199
}

0 commit comments

Comments
 (0)