From ac9dfb300428976fb7666f2edaf3e20f43d5edc5 Mon Sep 17 00:00:00 2001 From: Yvo Brevoort Date: Wed, 9 Jul 2025 15:48:27 +0200 Subject: [PATCH 1/8] add test for middleware --- tests/phpunit/MiddlewareTest.php | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/phpunit/MiddlewareTest.php diff --git a/tests/phpunit/MiddlewareTest.php b/tests/phpunit/MiddlewareTest.php new file mode 100644 index 0000000..e8b7510 --- /dev/null +++ b/tests/phpunit/MiddlewareTest.php @@ -0,0 +1,44 @@ +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)); + } + } From 6b4cfa54b12c5624bb3868235092f4a20e4e4a89 Mon Sep 17 00:00:00 2001 From: Yvo Brevoort Date: Wed, 9 Jul 2025 15:48:36 +0200 Subject: [PATCH 2/8] remove duplicate header --- lib/Middleware.php | 1 - 1 file changed, 1 deletion(-) 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); From 8457ac8d6e57b6ad0ad8569fd10ca6bed137f2e2 Mon Sep 17 00:00:00 2001 From: Yvo Brevoort Date: Wed, 9 Jul 2025 15:56:37 +0200 Subject: [PATCH 3/8] remove unused var --- tests/phpunit/MiddlewareTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpunit/MiddlewareTest.php b/tests/phpunit/MiddlewareTest.php index e8b7510..08cf7ec 100644 --- a/tests/phpunit/MiddlewareTest.php +++ b/tests/phpunit/MiddlewareTest.php @@ -4,7 +4,6 @@ use Pdsinterop\PhpSolid\Middleware; const PUBSUB_SERVER = "https://localhost:1234"; - $headers = []; function header($header) { MiddleWareTest::$headers[] = $header; } From 6712f1ab431dd5bfc210b37a8ffa2145fe5965c3 Mon Sep 17 00:00:00 2001 From: Yvo Brevoort Date: Wed, 9 Jul 2025 15:57:00 +0200 Subject: [PATCH 4/8] add session test --- TODO | 4 ++-- tests/phpunit/SessionTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/SessionTest.php diff --git a/TODO b/TODO index 8e014c9..39a4a1f 100644 --- a/TODO +++ b/TODO @@ -66,13 +66,13 @@ - [v] Util - [v] PasswordValidator - [v] User +- [v] Middleware +- [v] Session - [ ] Mailer - [ ] MailTemplateGenerator - [ ] MailTemplates - [ ] Server - [ ] StorageServer -- [-] Session - [-] SolidNotifications - [-] SolidPubSub -- [-] Middleware - [-] Db 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"); + } + } From 301f9718c1ce443990db18950ee5b37e7df9c532 Mon Sep 17 00:00:00 2001 From: Yvo Brevoort Date: Wed, 9 Jul 2025 16:01:01 +0200 Subject: [PATCH 5/8] add Db test --- tests/phpunit/DbTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/phpunit/DbTest.php 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); + } + } From 642b6d93f80b4b7c74a3a2bf1b589074b44e0034 Mon Sep 17 00:00:00 2001 From: Yvo Brevoort Date: Wed, 9 Jul 2025 16:01:18 +0200 Subject: [PATCH 6/8] update TODO --- TODO | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO b/TODO index 39a4a1f..abebfa1 100644 --- a/TODO +++ b/TODO @@ -68,6 +68,7 @@ - [v] User - [v] Middleware - [v] Session +- [v] Db - [ ] Mailer - [ ] MailTemplateGenerator - [ ] MailTemplates @@ -75,4 +76,3 @@ - [ ] StorageServer - [-] SolidNotifications - [-] SolidPubSub -- [-] Db From d8465e3edf4fa1d7a17a84db99c51b9b7b1b250c Mon Sep 17 00:00:00 2001 From: Yvo Brevoort Date: Wed, 9 Jul 2025 17:48:23 +0200 Subject: [PATCH 7/8] changes to make Mailer testable, tests for Mailer and a bugfix --- lib/MailTemplateGenerator.php | 2 +- lib/Mailer.php | 6 +- tests/phpunit/MailerTest.php | 132 ++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/MailerTest.php 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) {
- {buttonText} + {buttonText}
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/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. + } + } From 0fb56fef5f3eef369f2b68b8bda18ba32961c991 Mon Sep 17 00:00:00 2001 From: Yvo Brevoort Date: Wed, 9 Jul 2025 17:49:12 +0200 Subject: [PATCH 8/8] update TODO --- TODO | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TODO b/TODO index abebfa1..a623fd7 100644 --- a/TODO +++ b/TODO @@ -59,7 +59,7 @@ - [v] solid-crud - [v] CI integration ------- Unit tests ----- +------ Unit/Integration tests ----- - [v] ClientRegistration - [v] JtiStore - [v] IpAttempts @@ -69,9 +69,9 @@ - [v] Middleware - [v] Session - [v] Db -- [ ] Mailer -- [ ] MailTemplateGenerator -- [ ] MailTemplates +- [v] Mailer +- [v] MailTemplateGenerator +- [v] MailTemplates - [ ] Server - [ ] StorageServer - [-] SolidNotifications