Skip to content

Commit 8dae1e5

Browse files
authored
Merge pull request #8 from pdsinterop/test/middleware
Tests for middleware, db, session, mailer
2 parents 9747434 + 0fb56fe commit 8dae1e5

File tree

8 files changed

+225
-10
lines changed

8 files changed

+225
-10
lines changed

TODO

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@
5959
- [v] solid-crud
6060
- [v] CI integration
6161

62-
------ Unit tests -----
62+
------ Unit/Integration tests -----
6363
- [v] ClientRegistration
6464
- [v] JtiStore
6565
- [v] IpAttempts
6666
- [v] Util
6767
- [v] PasswordValidator
6868
- [v] User
69-
- [ ] Mailer
70-
- [ ] MailTemplateGenerator
71-
- [ ] MailTemplates
69+
- [v] Middleware
70+
- [v] Session
71+
- [v] Db
72+
- [v] Mailer
73+
- [v] MailTemplateGenerator
74+
- [v] MailTemplates
7275
- [ ] Server
7376
- [ ] StorageServer
74-
- [-] Session
7577
- [-] SolidNotifications
7678
- [-] SolidPubSub
77-
- [-] Middleware
78-
- [-] Db

lib/MailTemplateGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private static function mailTemplateCallToActionButton($mailTokens) {
116116
<table width="100%" border="0" cellpadding="0" cellspacing="0">
117117
<tr>
118118
<th valign="top" align="center" style="padding: 0px 0px 30px 0px; text-align: center; font-weight: normal;">
119-
<a href="{buttonLink}" style="display: inline-block; box-sizing: border-box; border-radius: 8px; background-color: <?php echo $buttonBackgroundColor; ?>; padding: 14px 18px 14px 18px; vertical-align: top; text-align: center; text-decoration: none;" target="_blank"><span style="font-size: 20px;line-height: 30px;color:<?php echo $buttonTextColor; ?>;font-weight:500;font-style:normal;display:inline-block;vertical-align: top;"><span style="display:inline-block;"><span style="font-family: <?php echo $fontFamily; ?>;line-height: 150%;">{buttonText}</span></span></span></span>
119+
<a href="{buttonLink}" style="display: inline-block; box-sizing: border-box; border-radius: 8px; background-color: <?php echo $buttonBackgroundColor; ?>; padding: 14px 18px 14px 18px; vertical-align: top; text-align: center; text-decoration: none;" target="_blank"><span style="font-size: 20px;line-height: 30px;color:<?php echo $buttonTextColor; ?>;font-weight:500;font-style:normal;display:inline-block;vertical-align: top;"><span style="display:inline-block;"><span style="font-family: <?php echo $fontFamily; ?>;line-height: 150%;">{buttonText}</span></span></span></a>
120120
</th>
121121
</tr>
122122
</table>

lib/Mailer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
use PHPMailer\PHPMailer\PHPMailer;
66

77
class Mailer {
8-
private static function getMailer() {
8+
public static $mailer = null;
9+
public static function getMailer() {
10+
if (self::$mailer) {
11+
return self::$mailer;
12+
}
913
$mailer = new PHPMailer();
1014
// Settings
1115
$mailer->IsSMTP();

lib/Middleware.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static function cors() {
1818
header( 'Access-Control-Allow-Origin: ' . $corsAllowOrigin );
1919
header( 'Access-Control-Allow-Headers: ' . $corsAllowedHeaders );
2020
header( 'Access-Control-Allow-Methods: ' . $corsMethods);
21-
header( 'Access-Control-Allow-Headers: ' . $corsAllowedHeaders);
2221
header( 'Access-Control-Max-Age: ' . $corsMaxAge);
2322
header( 'Access-Control-Allow-Credentials: ' . $corsAllowCredentials);
2423
header( 'Access-Control-Expose-Headers: ' . $corsExposeHeaders);

tests/phpunit/DbTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid;
3+
4+
use Pdsinterop\PhpSolid\Db;
5+
6+
const DBPATH = ":memory:";
7+
class DbTest extends \PHPUnit\Framework\TestCase
8+
{
9+
public function testConnect() {
10+
Db::connect();
11+
$this->assertInstanceOf("PDO", Db::$pdo);
12+
}
13+
}

tests/phpunit/MailerTest.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid;
3+
4+
use Pdsinterop\PhpSolid\Mailer;
5+
6+
const MAILER = [
7+
"host" => "mailerHost",
8+
"user" => "mailerUser",
9+
"password" => "mailerPass",
10+
"port" => "1337",
11+
"from" => "alice@example.com"
12+
];
13+
14+
const MAILSTYLES = [];
15+
16+
const BASEURL = "https://example.com";
17+
18+
class MailerMock {
19+
public $Subject;
20+
public $Body;
21+
public $AltBody;
22+
public $addresses = [];
23+
24+
public function addAddress($address) {
25+
$this->addresses[] = $address;
26+
}
27+
public function send() {
28+
return true;
29+
}
30+
}
31+
32+
class MailerTest extends \PHPUnit\Framework\TestCase
33+
{
34+
public function testGetMailer() {
35+
$mailer = Mailer::getMailer();
36+
$this->assertInstanceOf('\PHPMailer\PHPMailer\PHPMailer', $mailer);
37+
$this->assertEquals($mailer->Host, MAILER['host']);
38+
$this->assertEquals($mailer->From, MAILER['from']);
39+
$this->assertEquals($mailer->Port, MAILER['port']);
40+
$this->assertEquals($mailer->Username, MAILER['user']);
41+
$this->assertEquals($mailer->Password, MAILER['password']);
42+
$this->assertEquals($mailer->SMTPAuth, true);
43+
$this->assertEquals($mailer->SMTPDebug, 0);
44+
$this->assertEquals($mailer->XMailer, null);
45+
$this->assertEquals($mailer->ContentType, "text/html");
46+
$this->assertEquals($mailer->Mailer, "smtp");
47+
}
48+
49+
public function testAccountCreated() {
50+
Mailer::$mailer = new MailerMock();
51+
Mailer::sendAccountCreated([
52+
'email' => 'alice@example.com',
53+
'webId' => 'aliceWebId'
54+
]);
55+
$this->assertContains("alice@example.com", Mailer::$mailer->addresses);
56+
$this->assertMatchesRegularExpression("/aliceWebId/", Mailer::$mailer->AltBody);
57+
$this->assertMatchesRegularExpression("/aliceWebId/", Mailer::$mailer->Body);
58+
$this->assertMatchesRegularExpression("/<!-- Header -->/", Mailer::$mailer->Body);
59+
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
60+
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
61+
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
62+
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
63+
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
64+
$this->assertMatchesRegularExpression("|</html>|", Mailer::$mailer->Body);
65+
$doc = new \DOMDocument();
66+
$doc->loadHTML(Mailer::$mailer->Body);
67+
$this->assertEquals("Welkom bij Solid!", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
68+
}
69+
70+
public function testVerify() {
71+
Mailer::$mailer = new MailerMock();
72+
Mailer::sendVerify([
73+
'email' => 'alice@example.com',
74+
'code' => '654321'
75+
]);
76+
$this->assertContains("alice@example.com", Mailer::$mailer->addresses);
77+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->AltBody);
78+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->Body);
79+
$this->assertMatchesRegularExpression("/<!-- Header -->/", Mailer::$mailer->Body);
80+
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
81+
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
82+
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
83+
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
84+
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
85+
$this->assertMatchesRegularExpression("|</html>|", Mailer::$mailer->Body);
86+
$doc = new \DOMDocument();
87+
$doc->loadHTML(Mailer::$mailer->Body);
88+
$this->assertEquals("Bevestig je e-mail", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
89+
}
90+
91+
public function testResetPassword() {
92+
Mailer::$mailer = new MailerMock();
93+
Mailer::sendResetPassword([
94+
'email' => 'alice@example.com',
95+
'code' => '654321'
96+
]);
97+
$this->assertContains("alice@example.com", Mailer::$mailer->addresses);
98+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->AltBody);
99+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->Body);
100+
$this->assertMatchesRegularExpression("/<!-- Header -->/", Mailer::$mailer->Body);
101+
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
102+
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
103+
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
104+
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
105+
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
106+
$this->assertMatchesRegularExpression("|</html>|", Mailer::$mailer->Body);
107+
$doc = new \DOMDocument();
108+
$doc->loadHTML(Mailer::$mailer->Body);
109+
$this->assertEquals("Wachtwoordherstel", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
110+
}
111+
112+
public function testDeleteAccount() {
113+
Mailer::$mailer = new MailerMock();
114+
Mailer::sendDeleteAccount([
115+
'email' => 'alice@example.com',
116+
'code' => '654321'
117+
]);
118+
$this->assertContains("alice@example.com", Mailer::$mailer->addresses);
119+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->AltBody);
120+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->Body);
121+
$this->assertMatchesRegularExpression("/<!-- Header -->/", Mailer::$mailer->Body);
122+
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
123+
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
124+
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
125+
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
126+
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
127+
$this->assertMatchesRegularExpression("|</html>|", Mailer::$mailer->Body);
128+
$doc = new \DOMDocument();
129+
$doc->loadHTML(Mailer::$mailer->Body);
130+
$this->assertEquals("Je account verwijderen", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
131+
}
132+
}

tests/phpunit/MiddlewareTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid;
3+
4+
use Pdsinterop\PhpSolid\Middleware;
5+
6+
const PUBSUB_SERVER = "https://localhost:1234";
7+
function header($header) {
8+
MiddleWareTest::$headers[] = $header;
9+
}
10+
11+
class MiddlewareTest extends \PHPUnit\Framework\TestCase
12+
{
13+
public static $headers = [];
14+
public function testCors() {
15+
Middleware::cors();
16+
$this->assertTrue(in_array("Access-Control-Allow-Origin: *", self::$headers));
17+
$this->assertTrue(in_array("Access-Control-Allow-Headers: *, allow, accept, authorization, content-type, dpop, slug, link", self::$headers));
18+
$this->assertTrue(in_array("Access-Control-Allow-Methods: GET, PUT, POST, OPTIONS, DELETE, PATCH", self::$headers));
19+
$this->assertTrue(in_array("Access-Control-Max-Age: 1728000", self::$headers));
20+
$this->assertTrue(in_array("Access-Control-Allow-Credentials: true", self::$headers));
21+
$this->assertTrue(in_array("Accept-Patch: text/n3", self::$headers));
22+
$this->assertTrue(in_array("Access-Control-Expose-Headers: Authorization, User, Location, Link, Vary, Last-Modified, ETag, Accept-Patch, Accept-Post, Updates-Via, Allow, WAC-Allow, Content-Length, WWW-Authenticate, MS-Author-Via", self::$headers));
23+
}
24+
25+
public function testCorsWithOrigin() {
26+
$origin = "https://example.com";
27+
$_REQUEST['HTTP_ORIGIN'] = $origin;
28+
29+
Middleware::cors();
30+
$this->assertTrue(in_array("Access-Control-Allow-Origin: $origin", self::$headers));
31+
$this->assertTrue(in_array("Access-Control-Allow-Headers: *, allow, accept, authorization, content-type, dpop, slug, link", self::$headers));
32+
$this->assertTrue(in_array("Access-Control-Allow-Methods: GET, PUT, POST, OPTIONS, DELETE, PATCH", self::$headers));
33+
$this->assertTrue(in_array("Access-Control-Max-Age: 1728000", self::$headers));
34+
$this->assertTrue(in_array("Access-Control-Allow-Credentials: true", self::$headers));
35+
$this->assertTrue(in_array("Accept-Patch: text/n3", self::$headers));
36+
$this->assertTrue(in_array("Access-Control-Expose-Headers: Authorization, User, Location, Link, Vary, Last-Modified, ETag, Accept-Patch, Accept-Post, Updates-Via, Allow, WAC-Allow, Content-Length, WWW-Authenticate, MS-Author-Via", self::$headers));
37+
}
38+
39+
public function testPubSub() {
40+
Middleware::pubsub();
41+
$this->assertTrue(in_array("updates-via: " . PUBSUB_SERVER, self::$headers));
42+
}
43+
}

tests/phpunit/SessionTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid;
3+
4+
use Pdsinterop\PhpSolid\Session;
5+
6+
function session_start($options=[]) {
7+
SessionTest::$sessionOptions = $options;
8+
}
9+
10+
class SessionTest extends \PHPUnit\Framework\TestCase
11+
{
12+
public static $sessionOptions = [];
13+
public function testStart() {
14+
Session::start("alice");
15+
$this->assertEquals($_SESSION['username'], "alice");
16+
$this->assertEquals(self::$sessionOptions, ['cookie_lifetime' => 86400]);
17+
}
18+
19+
public function testGetLoggedInUser() {
20+
Session::start("alice");
21+
$user = Session::getLoggedInUser();
22+
$this->assertEquals($user, "alice");
23+
}
24+
}

0 commit comments

Comments
 (0)