diff --git a/TODO b/TODO
index 8e014c9..a623fd7 100644
--- a/TODO
+++ b/TODO
@@ -59,20 +59,20 @@
- [v] solid-crud
- [v] CI integration
------- Unit tests -----
+------ Unit/Integration tests -----
- [v] ClientRegistration
- [v] JtiStore
- [v] IpAttempts
- [v] Util
- [v] PasswordValidator
- [v] User
-- [ ] Mailer
-- [ ] MailTemplateGenerator
-- [ ] MailTemplates
+- [v] Middleware
+- [v] Session
+- [v] Db
+- [v] Mailer
+- [v] MailTemplateGenerator
+- [v] MailTemplates
- [ ] Server
- [ ] StorageServer
-- [-] Session
- [-] SolidNotifications
- [-] SolidPubSub
-- [-] Middleware
-- [-] Db
diff --git a/lib/MailTemplateGenerator.php b/lib/MailTemplateGenerator.php
index d6e4ee7..7feb10e 100644
--- a/lib/MailTemplateGenerator.php
+++ b/lib/MailTemplateGenerator.php
@@ -116,7 +116,7 @@ private static function mailTemplateCallToActionButton($mailTokens) {
diff --git a/lib/Mailer.php b/lib/Mailer.php
index 08dc7ae..b38e032 100644
--- a/lib/Mailer.php
+++ b/lib/Mailer.php
@@ -5,7 +5,11 @@
use PHPMailer\PHPMailer\PHPMailer;
class Mailer {
- private static function getMailer() {
+ public static $mailer = null;
+ public static function getMailer() {
+ if (self::$mailer) {
+ return self::$mailer;
+ }
$mailer = new PHPMailer();
// Settings
$mailer->IsSMTP();
diff --git a/lib/Middleware.php b/lib/Middleware.php
index a572af3..76cb63c 100644
--- a/lib/Middleware.php
+++ b/lib/Middleware.php
@@ -18,7 +18,6 @@ public static function cors() {
header( 'Access-Control-Allow-Origin: ' . $corsAllowOrigin );
header( 'Access-Control-Allow-Headers: ' . $corsAllowedHeaders );
header( 'Access-Control-Allow-Methods: ' . $corsMethods);
- header( 'Access-Control-Allow-Headers: ' . $corsAllowedHeaders);
header( 'Access-Control-Max-Age: ' . $corsMaxAge);
header( 'Access-Control-Allow-Credentials: ' . $corsAllowCredentials);
header( 'Access-Control-Expose-Headers: ' . $corsExposeHeaders);
diff --git a/tests/phpunit/DbTest.php b/tests/phpunit/DbTest.php
new file mode 100644
index 0000000..b4e1d07
--- /dev/null
+++ b/tests/phpunit/DbTest.php
@@ -0,0 +1,13 @@
+assertInstanceOf("PDO", Db::$pdo);
+ }
+ }
diff --git a/tests/phpunit/MailerTest.php b/tests/phpunit/MailerTest.php
new file mode 100644
index 0000000..97240ee
--- /dev/null
+++ b/tests/phpunit/MailerTest.php
@@ -0,0 +1,132 @@
+ "mailerHost",
+ "user" => "mailerUser",
+ "password" => "mailerPass",
+ "port" => "1337",
+ "from" => "alice@example.com"
+ ];
+
+ const MAILSTYLES = [];
+
+ const BASEURL = "https://example.com";
+
+ class MailerMock {
+ public $Subject;
+ public $Body;
+ public $AltBody;
+ public $addresses = [];
+
+ public function addAddress($address) {
+ $this->addresses[] = $address;
+ }
+ public function send() {
+ return true;
+ }
+ }
+
+ class MailerTest extends \PHPUnit\Framework\TestCase
+ {
+ public function testGetMailer() {
+ $mailer = Mailer::getMailer();
+ $this->assertInstanceOf('\PHPMailer\PHPMailer\PHPMailer', $mailer);
+ $this->assertEquals($mailer->Host, MAILER['host']);
+ $this->assertEquals($mailer->From, MAILER['from']);
+ $this->assertEquals($mailer->Port, MAILER['port']);
+ $this->assertEquals($mailer->Username, MAILER['user']);
+ $this->assertEquals($mailer->Password, MAILER['password']);
+ $this->assertEquals($mailer->SMTPAuth, true);
+ $this->assertEquals($mailer->SMTPDebug, 0);
+ $this->assertEquals($mailer->XMailer, null);
+ $this->assertEquals($mailer->ContentType, "text/html");
+ $this->assertEquals($mailer->Mailer, "smtp");
+ }
+
+ public function testAccountCreated() {
+ Mailer::$mailer = new MailerMock();
+ Mailer::sendAccountCreated([
+ 'email' => 'alice@example.com',
+ 'webId' => 'aliceWebId'
+ ]);
+ $this->assertContains("alice@example.com", Mailer::$mailer->addresses);
+ $this->assertMatchesRegularExpression("/aliceWebId/", Mailer::$mailer->AltBody);
+ $this->assertMatchesRegularExpression("/aliceWebId/", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("|Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $doc = new \DOMDocument();
+ $doc->loadHTML(Mailer::$mailer->Body);
+ $this->assertEquals("Welkom bij Solid!", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
+ }
+
+ public function testVerify() {
+ Mailer::$mailer = new MailerMock();
+ Mailer::sendVerify([
+ 'email' => 'alice@example.com',
+ 'code' => '654321'
+ ]);
+ $this->assertContains("alice@example.com", Mailer::$mailer->addresses);
+ $this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->AltBody);
+ $this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("|Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $doc = new \DOMDocument();
+ $doc->loadHTML(Mailer::$mailer->Body);
+ $this->assertEquals("Bevestig je e-mail", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
+ }
+
+ public function testResetPassword() {
+ Mailer::$mailer = new MailerMock();
+ Mailer::sendResetPassword([
+ 'email' => 'alice@example.com',
+ 'code' => '654321'
+ ]);
+ $this->assertContains("alice@example.com", Mailer::$mailer->addresses);
+ $this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->AltBody);
+ $this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("|Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $doc = new \DOMDocument();
+ $doc->loadHTML(Mailer::$mailer->Body);
+ $this->assertEquals("Wachtwoordherstel", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
+ }
+
+ public function testDeleteAccount() {
+ Mailer::$mailer = new MailerMock();
+ Mailer::sendDeleteAccount([
+ 'email' => 'alice@example.com',
+ 'code' => '654321'
+ ]);
+ $this->assertContains("alice@example.com", Mailer::$mailer->addresses);
+ $this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->AltBody);
+ $this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("//", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $this->assertMatchesRegularExpression("|Body);
+ $this->assertMatchesRegularExpression("||", Mailer::$mailer->Body);
+ $doc = new \DOMDocument();
+ $doc->loadHTML(Mailer::$mailer->Body);
+ $this->assertEquals("Je account verwijderen", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
+ }
+ }
diff --git a/tests/phpunit/MiddlewareTest.php b/tests/phpunit/MiddlewareTest.php
new file mode 100644
index 0000000..08cf7ec
--- /dev/null
+++ b/tests/phpunit/MiddlewareTest.php
@@ -0,0 +1,43 @@
+assertTrue(in_array("Access-Control-Allow-Origin: *", self::$headers));
+ $this->assertTrue(in_array("Access-Control-Allow-Headers: *, allow, accept, authorization, content-type, dpop, slug, link", self::$headers));
+ $this->assertTrue(in_array("Access-Control-Allow-Methods: GET, PUT, POST, OPTIONS, DELETE, PATCH", self::$headers));
+ $this->assertTrue(in_array("Access-Control-Max-Age: 1728000", self::$headers));
+ $this->assertTrue(in_array("Access-Control-Allow-Credentials: true", self::$headers));
+ $this->assertTrue(in_array("Accept-Patch: text/n3", self::$headers));
+ $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));
+ }
+
+ public function testCorsWithOrigin() {
+ $origin = "https://example.com";
+ $_REQUEST['HTTP_ORIGIN'] = $origin;
+
+ Middleware::cors();
+ $this->assertTrue(in_array("Access-Control-Allow-Origin: $origin", self::$headers));
+ $this->assertTrue(in_array("Access-Control-Allow-Headers: *, allow, accept, authorization, content-type, dpop, slug, link", self::$headers));
+ $this->assertTrue(in_array("Access-Control-Allow-Methods: GET, PUT, POST, OPTIONS, DELETE, PATCH", self::$headers));
+ $this->assertTrue(in_array("Access-Control-Max-Age: 1728000", self::$headers));
+ $this->assertTrue(in_array("Access-Control-Allow-Credentials: true", self::$headers));
+ $this->assertTrue(in_array("Accept-Patch: text/n3", self::$headers));
+ $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));
+ }
+
+ public function testPubSub() {
+ Middleware::pubsub();
+ $this->assertTrue(in_array("updates-via: " . PUBSUB_SERVER, self::$headers));
+ }
+ }
diff --git a/tests/phpunit/SessionTest.php b/tests/phpunit/SessionTest.php
new file mode 100644
index 0000000..cf176e0
--- /dev/null
+++ b/tests/phpunit/SessionTest.php
@@ -0,0 +1,24 @@
+assertEquals($_SESSION['username'], "alice");
+ $this->assertEquals(self::$sessionOptions, ['cookie_lifetime' => 86400]);
+ }
+
+ public function testGetLoggedInUser() {
+ Session::start("alice");
+ $user = Session::getLoggedInUser();
+ $this->assertEquals($user, "alice");
+ }
+ }