aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/Entity/RegistrationTest.php
blob: 3e44125455a93d32d672e1077b74bfde4d8a0535 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?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);
	}

	public function test_get_registration_by_id(): void {
		$target = $this->fixtures['register'][0];

		$reg = Registration::get(intval($target['reg_id']));

		$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 ]
		];
	}
}