diff options
author | Marcos Zuriaga <wolfi@wolfi.es> | 2016-10-15 22:03:50 +0300 |
---|---|---|
committer | Marcos Zuriaga <wolfi@wolfi.es> | 2016-10-15 22:03:50 +0300 |
commit | 8a30328a7343ca913787bcdec7eb572acfc126f0 (patch) | |
tree | 5b4217fcf762a5870cb22cedf45438ebd0c049d6 /tests | |
parent | 41b0005e7c64e83d374791538335a6d05ce0f980 (diff) |
Added partial test
Fixed bug with null data in the mock datasets
Edited mock data so there it's some expired credentials
Added some phpdocs
Diffstat (limited to 'tests')
-rw-r--r-- | tests/db/DatabaseHelperTest.php | 13 | ||||
-rw-r--r-- | tests/db/dumps/oc_passman_credentials.xml | 2 | ||||
-rw-r--r-- | tests/unit/lib/Db/CredentialMapperTest.php | 125 |
3 files changed, 136 insertions, 4 deletions
diff --git a/tests/db/DatabaseHelperTest.php b/tests/db/DatabaseHelperTest.php index 6181f193..edc50673 100644 --- a/tests/db/DatabaseHelperTest.php +++ b/tests/db/DatabaseHelperTest.php @@ -81,7 +81,14 @@ abstract class DatabaseHelperTest extends PHPUnit_Extensions_Database_TestCase { $qb->insert($table_no_prefix); foreach ($row as $key => $value){ - $qb->setValue($key, is_numeric($value) ? $value : ("'{$value}'")); + if (is_null($value)) { + $value = 'NULL'; + } + else if (!is_numeric($value)) { + $value = "'{$value}'"; + } + + $qb->setValue($key, $value); } $qb->execute(); @@ -114,7 +121,7 @@ abstract class DatabaseHelperTest extends PHPUnit_Extensions_Database_TestCase { $result = []; for ($i = 0; $i < $rows; $i++) { $row = $table->getRow($i); - if ($row[$field_name] === $value_match){ + if ($row[$field_name] == $value_match){ $result[] = $row; } } @@ -132,7 +139,7 @@ abstract class DatabaseHelperTest extends PHPUnit_Extensions_Database_TestCase { $ret = []; foreach ($dataset as $value){ - if ($value[$field_name] === $value_match){ + if ($value[$field_name] == $value_match){ $ret[] = $value; } } diff --git a/tests/db/dumps/oc_passman_credentials.xml b/tests/db/dumps/oc_passman_credentials.xml index fd0d05cd..d2278ef8 100644 --- a/tests/db/dumps/oc_passman_credentials.xml +++ b/tests/db/dumps/oc_passman_credentials.xml @@ -90,7 +90,7 @@ <field name="url">eyJpdiI6Ikg2UC96K1dmdGRZN0VhYU1XV1ZsQlEiLCJ2IjoxLCJpdGVyIjoxMDAwLCJrcyI6MjU2LCJ0cyI6NjQsIm1vZGUiOiJjY20iLCJhZGF0YSI6IiIsImNpcGhlciI6ImFlcyIsInNhbHQiOiJEN1Y1Z0VYbWt6NCIsImN0IjoibFhvV25sVTluZStDQklkNCJ9</field> <field name="favicon" xsi:nil="true" /> <field name="renew_interval" xsi:nil="true" /> - <field name="expire_time">0</field> + <field name="expire_time">150</field> <field name="delete_time">0</field> <field name="files">eyJpdiI6Ijh4UTJ4ZlF5YXlJN25CYXAvUGtMWGciLCJ2IjoxLCJpdGVyIjoxMDAwLCJrcyI6MjU2LCJ0cyI6NjQsIm1vZGUiOiJjY20iLCJhZGF0YSI6IiIsImNpcGhlciI6ImFlcyIsInNhbHQiOiJEN1Y1Z0VYbWt6NCIsImN0IjoiQU9oUDBiamE0ZmxGL1EifQ==</field> <field name="custom_fields">eyJpdiI6Ik1QM1RPZXl1dWNzZEtiVS9ucm0vaEEiLCJ2IjoxLCJpdGVyIjoxMDAwLCJrcyI6MjU2LCJ0cyI6NjQsIm1vZGUiOiJjY20iLCJhZGF0YSI6IiIsImNpcGhlciI6ImFlcyIsInNhbHQiOiJEN1Y1Z0VYbWt6NCIsImN0IjoiSlBlMExLMWIvdTdMTkEifQ==</field> diff --git a/tests/unit/lib/Db/CredentialMapperTest.php b/tests/unit/lib/Db/CredentialMapperTest.php new file mode 100644 index 00000000..e9f95634 --- /dev/null +++ b/tests/unit/lib/Db/CredentialMapperTest.php @@ -0,0 +1,125 @@ +<?php + +/** + * + * Date: 15/10/16 + * Time: 20:06 + * + * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel (wolfi@wolfi.es) + * @license AGPLv3 + */ +use \OCA\Passman\Db\CredentialMapper; +use \OCA\Passman\Db\Credential; +use \OCA\Passman\Utility\Utils; + +/** + * @coversDefaultClass \OCA\Passman\Db\CredentialMapper + */ +class CredentialMapperTest extends DatabaseHelperTest { + CONST TABLES = [ + 'oc_passman_credentials' + ]; + + /** + * @var CredentialMapper + */ + protected $mapper; + + /** + * @var PHPUnit_Extensions_Database_DataSet_ITable + */ + protected $dataset; + + /** + * @after + */ + public function setUp() { + parent::setUp(); + $this->mapper = new CredentialMapper($this->db, new Utils()); + $this->dataset = $this->getTableDataset(self::TABLES[0]); + } + + /** + * This function should return the table name, for example + * for a test running on oc_passman_vaults it shall return ["oc_passman_vaults"] + * + * @internal + * @return string[] + */ + public function getTablesNames() { + return self::TABLES; + } + + /** + * @covers ::getCredentialsByVaultId + */ + public function testGetCredentialsByVaultId() { + $expected = $this->findInDataset( + self::TABLES[0], + 'vault_id', + $this->dataset->getRow(0)['vault_id'] + ); + + $expected = $this->filterDataset($expected, 'vault_id', $expected[0]['vault_id']); + + /** + * @var Credential[] $expected + */ + foreach ($expected as &$value) $value = Credential::fromRow($value); + + $data = $this->mapper->getCredentialsByVaultId( + $expected[0]->getVaultId(), + $expected[0]->getUserId() + ); + + $this->assertCount(count($expected), $data); + $this->assertEquals($expected, $data); + } + + /** + * @covers ::getRandomCredentialByVaultId + */ + public function testGetRandomCredentialByVaultId() { + $row = $this->dataset->getRow(0); + + $data = $this->mapper->getRandomCredentialByVaultId( + $row['vault_id'], + $row['user_id'] + ); + $this->assertCount(1, $data); + + $tmp = $this->findInDataset(self::TABLES[0], 'vault_id', $row['vault_id']); + $tmp = $this->filterDataset($tmp, 'user_id', $row['user_id']); + $tmp = $this->filterDataset($tmp, 'id', $data[0]->getId()); + $this->assertCount(count($tmp), $data); + + $tmp[0] = Credential::fromRow($tmp[0]); + $this->assertEquals($tmp, $data); + } + + /** + * @covers ::getExpiredCredentials + */ + public function testGetExpiredCredentials() { + $expired = []; + + for ($i = 0; $i < $this->dataset->getRowCount(); $i++) { + $row = $this->dataset->getRow($i); + if ($row['expire_time'] > 0 && $row['expire_time'] < Utils::getTime()){ + $expired[] = Credential::fromRow($row); + } + } + + $data = $this->mapper->getExpiredCredentials(Utils::getTime()); + $this->assertNotCount(0, $data, "Not any expired credentials in the dataset?"); + $this->assertCount(count($expired), $data); + $this->assertEquals($expired, $data); + } + +// /** +// * @covers ::getCredentialById +// */ +// public function testGetCredentialById() { +// +// } +}
\ No newline at end of file |