Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/MailTemplateGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private static function mailTemplateCallToActionButton($mailTokens) {
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<th valign="top" align="center" style="padding: 0px 0px 30px 0px; text-align: center; font-weight: normal;">
<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>
<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>
</th>
</tr>
</table>
Expand Down
6 changes: 5 additions & 1 deletion lib/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion lib/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions tests/phpunit/DbTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Pdsinterop\PhpSolid;

use Pdsinterop\PhpSolid\Db;

const DBPATH = ":memory:";
class DbTest extends \PHPUnit\Framework\TestCase
{
public function testConnect() {
Db::connect();
$this->assertInstanceOf("PDO", Db::$pdo);
}
}
132 changes: 132 additions & 0 deletions tests/phpunit/MailerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
namespace Pdsinterop\PhpSolid;

use Pdsinterop\PhpSolid\Mailer;

const MAILER = [
"host" => "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("/<!-- Header -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|</html>|", 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("/<!-- Header -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|</html>|", 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("/<!-- Header -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|</html>|", 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("/<!-- Header -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
$this->assertMatchesRegularExpression("|</html>|", 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.
}
}
43 changes: 43 additions & 0 deletions tests/phpunit/MiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Pdsinterop\PhpSolid;

use Pdsinterop\PhpSolid\Middleware;

const PUBSUB_SERVER = "https://localhost:1234";
function header($header) {
MiddleWareTest::$headers[] = $header;
}

class MiddlewareTest extends \PHPUnit\Framework\TestCase
{
public static $headers = [];
public function testCors() {
Middleware::cors();
$this->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));
}
}
24 changes: 24 additions & 0 deletions tests/phpunit/SessionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Pdsinterop\PhpSolid;

use Pdsinterop\PhpSolid\Session;

function session_start($options=[]) {
SessionTest::$sessionOptions = $options;
}

class SessionTest extends \PHPUnit\Framework\TestCase
{
public static $sessionOptions = [];
public function testStart() {
Session::start("alice");
$this->assertEquals($_SESSION['username'], "alice");
$this->assertEquals(self::$sessionOptions, ['cookie_lifetime' => 86400]);
}

public function testGetLoggedInUser() {
Session::start("alice");
$user = Session::getLoggedInUser();
$this->assertEquals($user, "alice");
}
}