diff options
Diffstat (limited to 'tests/unit/includes/dba')
-rw-r--r-- | tests/unit/includes/dba/DbaPdoTest.php | 140 | ||||
-rw-r--r-- | tests/unit/includes/dba/TransactionTest.php | 6 | ||||
-rw-r--r-- | tests/unit/includes/dba/_files/account.yml | 19 | ||||
-rw-r--r-- | tests/unit/includes/dba/_files/config.yml | 4 | ||||
-rw-r--r-- | tests/unit/includes/dba/_files/register.yml | 20 |
5 files changed, 186 insertions, 3 deletions
diff --git a/tests/unit/includes/dba/DbaPdoTest.php b/tests/unit/includes/dba/DbaPdoTest.php new file mode 100644 index 000000000..8a1a2b197 --- /dev/null +++ b/tests/unit/includes/dba/DbaPdoTest.php @@ -0,0 +1,140 @@ +<?php +/** + * Tests for `includes/dba_pdo.php`. + * + * SPDX-FileCopyrightText: 2024 Hubzilla Community + * SPDX-FileContributor: Harald Eilertsen + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\includes; + +use DBA; +use PDO; +use PDOStatement; +use PHPUnit\Framework\Attributes\DataProvider; +use Zotlabs\Tests\Unit\UnitTestCase; + +class DbaPdoTest extends UnitTestCase +{ + public function testInsertingRowWithRturningClauseReturnsInsertedRow(): void + { + // MySQL does not support the `returning` clause, so we skip the test + // for that DB backend. + $this->skipIfMySQL(); + + // Let's manually insert a row in the config table. + // This is just because it's a conventient table to test + // against + $res = q(<<<SQL + INSERT INTO config (cat, k, v) + VALUES ('test', 'a key', 'A value') + RETURNING * + SQL); + + $this->assertIsArray($res); + $this->assertIsArray($res[0]); + $this->assertTrue($res[0]['id'] > 0); + $this->assertEquals('test', $res[0]['cat']); + $this->assertEquals('a key', $res[0]['k']); + $this->assertEquals('A value', $res[0]['v']); + } + + #[DataProvider('insertRowProvider')] + public function testInsertRow(string $table, array $data, string $id): void + { + $res = DBA::$dba->insert($table, $data, $id); + + $this->assertIsArray($res); + + // Make sure the result contains the expected id + $this->assertArrayHasKey($id, $res); + + foreach ($data as $key => $value) { + $this->assertEquals($value, $res[$key]); + } + } + + #[DataProvider('insertRowProvider')] + public function testInsertShouldReturnFalseIfInsertFails( + string $table, + array $data, + string $id + ): void + { + $res1 = DBA::$dba->insert($table, $data, $id); + $this->assertIsArray($res1); + + // Inserting the same row again should fail. + $res2 = DBA::$dba->insert($table, $data, $id); + $this->assertFalse($res2); + } + + /** + * Dataprovider for testInertRow. + * + * @return array An array of [ $table, $data, $id ] elements. + */ + public static function insertRowProvider(): array + { + return [ + 'table with numeric primary id' => [ + 'config', + [ 'cat' => 'test', 'k' => 'a key', 'v' => 'A value' ], + 'id', + ], + 'table with text primary id' => [ + 'cache', + [ 'k' => 'some key', 'v' => 'cached value', 'updated' => date('Y-m-d H:i:s')], + 'k', + ], + ]; + } + + public function testUpdateRow(): void + { + // Let's fetch a row from the config table + $res = q("SELECT * FROM config WHERE cat = 'system' AND k = 'baseurl'"); + + $this->assertIsArray($res); + $this->assertIsArray($res[0]); + + $row = $res[0]; + + // Update the baseurl + $updated = DBA::$dba->update( + 'config', + [ 'v' => 'https://some.other_site.test/' ], + 'id', + $row['id'] + ); + + $this->assertTrue($updated); + + // Verify that the record was updated + $updated_res = q("SELECT * FROM config WHERE cat = 'system' AND k = 'baseurl'"); + $this->assertIsArray($updated_res); + + $updated_row = $updated_res[0]; + + $this->assertIsArray($updated_row); + $this->assertEquals($row['id'], $updated_row['id']); + $this->assertEquals('system', $updated_row['cat']); + $this->assertEquals('baseurl', $updated_row['k']); + $this->assertEquals('https://some.other_site.test/', $updated_row['v']); + } + + /** + * Mark the test as skipped if the current db is MySQL. + */ + private function skipIfMySQL(): void { + $driver = DBA::$dba->db->getAttribute(PDO::ATTR_DRIVER_NAME); + $version = DBA::$dba->db->getAttribute(PDO::ATTR_SERVER_VERSION); + + if ($driver === 'mysql' && stripos($version, 'mariadb') === false) { + $this->markTestSkipped("RETURNING clause not supported for {$driver}"); + } + + } +} diff --git a/tests/unit/includes/dba/TransactionTest.php b/tests/unit/includes/dba/TransactionTest.php index 99e3f459d..0b986c6aa 100644 --- a/tests/unit/includes/dba/TransactionTest.php +++ b/tests/unit/includes/dba/TransactionTest.php @@ -24,8 +24,8 @@ require_once 'tests/fakes/fake_dba.php'; require_once 'include/dba/dba_transaction.php'; -use \PHPUnit\Framework\TestCase; -use \Zotlabs\Tests\Fakes\FakeDba; +use PHPUnit\Framework\TestCase; +use Zotlabs\Tests\Fakes\FakeDba; /** * Test database transactions. @@ -39,7 +39,7 @@ class DbaTransactionTest extends TestCase { private $pdo_stub; public function setUp(): void { - $this->pdo_stub = $this->createStub(PDO::class); + $this->pdo_stub = $this->createMock(PDO::class); } diff --git a/tests/unit/includes/dba/_files/account.yml b/tests/unit/includes/dba/_files/account.yml index 344bdb799..9c3d00ec8 100644 --- a/tests/unit/includes/dba/_files/account.yml +++ b/tests/unit/includes/dba/_files/account.yml @@ -3,7 +3,26 @@ account: account_id: 42 account_email: "hubzilla@example.com" account_language: "no" + account_level: 5 + account_flags: 0 - account_id: 43 account_email: "hubzilla@example.org" account_language: "de" + account_level: 5 + account_flags: 1 + - + account_id: 44 + account_email: "blocked@example.org" + account_level: 5 + account_flags: 2 + - + account_id: 45 + account_email: "pending@example.org" + account_level: 5 + account_flags: 0x10 + - + account_id: 46 + account_email: "unverified@example.org" + account_level: 5 + account_flags: 0x11 diff --git a/tests/unit/includes/dba/_files/config.yml b/tests/unit/includes/dba/_files/config.yml index e93486857..ac3c8acb0 100644 --- a/tests/unit/includes/dba/_files/config.yml +++ b/tests/unit/includes/dba/_files/config.yml @@ -2,6 +2,10 @@ config: - cat: system + k: baseurl + v: https://hubzilla.test + - + cat: system k: do_not_check_dns v: true - diff --git a/tests/unit/includes/dba/_files/register.yml b/tests/unit/includes/dba/_files/register.yml new file mode 100644 index 000000000..2ef1a5365 --- /dev/null +++ b/tests/unit/includes/dba/_files/register.yml @@ -0,0 +1,20 @@ +--- +register: + - + reg_vital: 1 + reg_flags: 0x10 + reg_did2: 'verified@example.com' + reg_email: 'verified@example.com' + reg_hash: '123' + reg_uid: 45 + reg_pass: 'verify' + reg_stuff: '' + - + reg_vital: 1 + reg_flags: 0x11 + reg_did2: 'unverified@example.com' + reg_email: 'unverified@example.com' + reg_hash: '666' + reg_uid: 46 + reg_pass: 'verify' + reg_stuff: '' |