aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2024-11-21 15:55:36 +0100
committerHarald Eilertsen <haraldei@anduin.net>2025-06-29 13:26:21 +0200
commita7cc2293fd77a0fa45cd42fb06586c253b3413a3 (patch)
treefa50dcdb1f8418872cf28c9272e272b6a76d5be9 /tests/unit
parentdd107e70caf1a63ae3903ee441678e615ab1b5d1 (diff)
downloadvolse-hubzilla-a7cc2293fd77a0fa45cd42fb06586c253b3413a3.tar.gz
volse-hubzilla-a7cc2293fd77a0fa45cd42fb06586c253b3413a3.tar.bz2
volse-hubzilla-a7cc2293fd77a0fa45cd42fb06586c253b3413a3.zip
Add Entity\Registration class to handle registrations
Currently registrations are managed by direct database queries spread accross several functions in `include/account.php` and various other locations. This makes it difficult to reason about how registrations are handled and manipulated throughout the application. This patch addresses this by creating a class that defines a registration as an object, and that is meant to be the _only_ place where registrations are managed, manipulated and persisted to or read from the database.
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/Entity/RegistrationTest.php102
-rw-r--r--tests/unit/includes/dba/_files/register.yml15
2 files changed, 116 insertions, 1 deletions
diff --git a/tests/unit/Entity/RegistrationTest.php b/tests/unit/Entity/RegistrationTest.php
new file mode 100644
index 000000000..31e421914
--- /dev/null
+++ b/tests/unit/Entity/RegistrationTest.php
@@ -0,0 +1,102 @@
+<?php
+/*
+ * SPDX-FileCopyrightText: 2024 Hubzilla Community
+ * SPDX-FileContributor: Harald Eilertsen
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+namespace Zotlabs\Tests\Entity\Entity;
+
+use PHPUnit\Framework\Attributes\DataProvider;
+use Zotlabs\Entity\Registration;
+use Zotlabs\Tests\Unit\UnitTestCase;
+
+class RegistrationTest extends UnitTestCase {
+
+ public function test_create_new_registration_adds_it_to_db(): void {
+ $count = Registration::count();
+
+ // For some reason 'reg_stuff' is the only column withoug a
+ // default setting.
+ $reg = Registration::create([
+ 'reg_stuff' => 'stuff',
+ ]);
+
+ $this->assertEquals($count + 1, Registration::count());
+ $this->assertInstanceOf(Registration::class, $reg);
+ }
+
+ public function test_getting_registration_from_db_works(): void {
+ $target = $this->fixtures['register'][0];
+
+ $reg = Registration::find_by_hash($target['reg_hash']);
+
+ $this->assertInstanceOf(Registration::class, $reg);
+
+ $this->assertEquals($target['reg_uid'], $reg->uid);
+ $this->assertEquals($target['reg_hash'], $reg->hash);
+ $this->assertEquals($target['reg_lang'], $reg->lang);
+ }
+
+ #[DataProvider('findByHashProvider')]
+ public function test_find_by_hash(string $hash, bool $expect_success): void {
+ $reg = Registration::find_by_hash($hash);
+
+ if ($expect_success) {
+ $this->assertInstanceOf(Registration::class, $reg);
+ } else {
+ $this->assertNull($reg);
+ }
+ }
+
+ public static function findByHashProvider(): array {
+ return [
+ 'valid hash should be found' => [ '666', true ],
+ 'valid hash but not in db' => [ '00DEADBEEF77', false ],
+ 'invalid hash' => [ '\' or 1 = 1; --', false ],
+ ];
+ }
+
+ #[DataProvider('findByEmailProvider')]
+ public function test_find_by_email(string $email, bool $expect_success): void {
+ $reg = Registration::find_by_email($email);
+
+ if ($expect_success) {
+ $this->assertInstanceOf(Registration::class, $reg);
+ } else {
+ $this->assertNull($reg);
+ }
+ }
+
+ public static function findByEmailProvider(): array {
+ return [
+ 'email found' => [ 'verified@example.com', true ],
+ 'email not found' => [ 'invalid@example.test', false ],
+ 'ignore non vital entries' => [ 'unverified@example.com', false ],
+ 'ignore entry with wrong didx' => [ 'invited@example.com', false ],
+ ];
+ }
+
+ #[DataProvider('findByInviteCodeProvider')]
+ public function test_find_by_invite_code(string $invite_code, bool $expect_success): void {
+ $reg = Registration::find_by_invite_code($invite_code);
+
+ if ($expect_success) {
+ $this->assertInstanceOf(Registration::class, $reg);
+ } else {
+ $this->assertNull($reg);
+ }
+ }
+
+ public static function findByInviteCodeProvider(): array {
+ return [
+ 'no invite code returns null' => [ '', false ],
+ 'only whitespace returns null' => [ ' ', false ],
+ 'non exiting invite code returns null' => [ 'not invited', false ],
+ 'non vital entry return null' => [ '666', false ],
+ 'non invite entry returns null' => [ '123', false ],
+ 'valid invite code returns registration' => [ 'invite code x', true ]
+ ];
+ }
+}
diff --git a/tests/unit/includes/dba/_files/register.yml b/tests/unit/includes/dba/_files/register.yml
index 2ef1a5365..e8c439d92 100644
--- a/tests/unit/includes/dba/_files/register.yml
+++ b/tests/unit/includes/dba/_files/register.yml
@@ -3,18 +3,31 @@ register:
-
reg_vital: 1
reg_flags: 0x10
+ reg_didx: 'e'
reg_did2: 'verified@example.com'
reg_email: 'verified@example.com'
reg_hash: '123'
reg_uid: 45
reg_pass: 'verify'
+ reg_lang: 'nb'
reg_stuff: ''
-
- reg_vital: 1
+ reg_vital: 0
reg_flags: 0x11
+ reg_didx: 'e'
reg_did2: 'unverified@example.com'
reg_email: 'unverified@example.com'
reg_hash: '666'
reg_uid: 46
reg_pass: 'verify'
reg_stuff: ''
+ -
+ reg_vital: 1
+ reg_flags: 0x11
+ reg_didx: 'i'
+ reg_did2: 'invited@example.com'
+ reg_email: 'invited@example.com'
+ reg_hash: '138'
+ reg_uid: 0
+ reg_pass: 'verify'
+ reg_stuff: ''