Skip to content

Commit

Permalink
Add path accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jul 16, 2016
1 parent 709a353 commit eed6f62
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 15 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ KeePass databases change log

## ?.?.? / ????-??-??

* Added path accessor to groups and entries - @thekid

## 0.3.0 / 2016-07-16

* Fixed issue #1: HHVM XML parsing - @thekid
Expand Down
5 changes: 5 additions & 0 deletions src/main/php/info/keepass/Entry.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
use util\UUID;
use lang\ElementNotFoundException;

/**
* A password entry
*
* @test xp://info.keepass.unittest.EntryTest
*/
class Entry extends Object {

/** @return util.UUID */
Expand Down
11 changes: 8 additions & 3 deletions src/main/php/info/keepass/Group.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
use util\UUID;
use lang\ElementNotFoundException;

/**
* A group
*
* @test xp://info.keepass.unittest.GroupTest
*/
class Group extends Object {

/** @return util.UUID */
Expand Down Expand Up @@ -39,7 +44,7 @@ public function password($title) {
public function passwords() {
if (isset($this->backing['Entry'])) {
foreach ($this->backing['Entry'] as $uuid => $entry) {
yield $entry['String']['Title'] => $entry['String']['Password'];
yield $this->path.$entry['String']['Title'] => $entry['String']['Password'];
}
}
}
Expand All @@ -52,7 +57,7 @@ public function passwords() {
public function groups() {
if (isset($this->backing['Group'])) {
foreach ($this->backing['Group'] as $uuid => $group) {
yield $this->decodeUUID($uuid) => new Group($group);
yield $this->decodeUUID($uuid) => new Group($group, $this->path.$group['Name']);
}
}
}
Expand All @@ -65,7 +70,7 @@ public function groups() {
public function entries() {
if (isset($this->backing['Entry'])) {
foreach ($this->backing['Entry'] as $uuid => $entry) {
yield $this->decodeUUID($uuid) => new Entry($entry);
yield $this->decodeUUID($uuid) => new Entry($entry, $this->path.$entry['String']['Title']);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/info/keepass/KeePassDatabase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function group($path) {
}
}

return new Group($structure);
return new Group($structure, $path);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/main/php/info/keepass/Object.class.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?php namespace info\keepass;

class Object {
protected $backing;
protected $backing, $path;

/**
* Creates a new group
*
* @param [:var] $backing
*/
public function __construct($backing) {
public function __construct($backing, $path) {
$this->backing= $backing;
$this->path= rtrim($path, '/').'/';
}

/** @return string */
public function path() { return '/' === $this->path ? '/' : substr($this->path, 0, -1); }

/**
* Decodes a UUID
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function entries_in_root() {
with ($this->database(), function($db) {
$this->assertEquals(
[
self::ID_ONE => new Entry($this->entries[self::ID_ONE]),
self::ID_TWO => new Entry($this->entries[self::ID_TWO])
self::ID_ONE => new Entry($this->entries[self::ID_ONE], '/Entry #1'),
self::ID_TWO => new Entry($this->entries[self::ID_TWO], '/Entry #2')
],
iterator_to_array($db->group('/')->entries())
);
Expand All @@ -70,8 +70,8 @@ public function all_passwords_in_root() {
with ($this->database(), function($db) {
$this->assertEquals(
[
'Entry #1' => $this->entries[self::ID_ONE]['String']['Password'],
'Entry #2' => $this->entries[self::ID_TWO]['String']['Password']
'/Entry #1' => $this->entries[self::ID_ONE]['String']['Password'],
'/Entry #2' => $this->entries[self::ID_TWO]['String']['Password']
],
iterator_to_array($db->passwords('/'))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function setUp() {
#[@test]
public function test_group() {
with ($this->database(), function($db) {
$this->assertEquals(new Group($this->group), $db->group('/Test'));
$this->assertEquals(new Group($this->group, '/Test'), $db->group('/Test'));
});
}

Expand All @@ -66,7 +66,7 @@ public function test_password() {
#[@test]
public function all_passwords_in_test_group() {
with ($this->database(), function($db) {
$this->assertEquals(['Test' => $this->entry['String']['Password']], iterator_to_array($db->passwords('/Test')));
$this->assertEquals(['/Test/Test' => $this->entry['String']['Password']], iterator_to_array($db->passwords('/Test')));
});
}

Expand All @@ -87,7 +87,7 @@ public function password_in_non_existant_folder() {
#[@test]
public function groups_in_root() {
with ($this->database(), function($db) {
$this->assertEquals([self::GROUPID => new Group($this->group)], iterator_to_array($db->groups()));
$this->assertEquals([self::GROUPID => new Group($this->group, '/Test')], iterator_to_array($db->groups()));
});
}

Expand All @@ -101,7 +101,7 @@ public function subgroups_of_test_group_are_empty() {
#[@test]
public function entries_in_test_group() {
with ($this->database(), function($db) {
$this->assertEquals([self::ENTRYID => new Entry($this->entry)], iterator_to_array($db->group('/Test')->entries()));
$this->assertEquals([self::ENTRYID => new Entry($this->entry, '/Test/Test')], iterator_to_array($db->group('/Test')->entries()));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function setUp() {
#[@test]
public function root_group() {
with ($this->database(), function($db) {
$this->assertEquals(new Group($this->root), $db->group('/'));
$this->assertEquals(new Group($this->root, '/'), $db->group('/'));
});
}

Expand Down
77 changes: 77 additions & 0 deletions src/test/php/info/keepass/unittest/EntryTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php namespace info\keepass\unittest;

use info\keepass\Entry;
use info\keepass\ProtectedValue;
use util\UUID;

class EntryTest extends \unittest\TestCase {
private $entry;

/** @return void */
public function setUp() {
$this->entry= [
'UUID' => 'fZhlFzAGRU24qsKpoxQ2Lg==',
'IconID' => '42',
'ForegroundColor' => null,
'BackgroundColor' => null,
'OverrideURL' => null,
'Tags' => null,
'Times' => '',
'String' => [
'Notes' => 'Notes',
'Password' => new ProtectedValue("\323\$c", "\274J\006"),
'Title' => 'Entry #1',
'URL' => 'http://example.com/',
'UserName' => 'test'
],
'History' => [
]
];
}

#[@test]
public function can_create() {
new Entry($this->entry, '/Entry #1');
}

#[@test]
public function path() {
$this->assertEquals('/Entry #1', (new Entry($this->entry, '/Entry #1'))->path());
}

#[@test]
public function uuid() {
$this->assertEquals(new UUID('7d986517-3006-454d-b8aa-c2a9a314362e'), (new Entry($this->entry, '/Entry #1'))->uuid());
}

#[@test]
public function title() {
$this->assertEquals('Entry #1', (new Entry($this->entry, '/Entry #1'))->title());
}

#[@test]
public function notes() {
$this->assertEquals('Notes', (new Entry($this->entry, '/Entry #1'))->notes());
}

#[@test]
public function url() {
$this->assertEquals('http://example.com/', (new Entry($this->entry, '/Entry #1'))->url());
}

#[@test]
public function username() {
$this->assertEquals('test', (new Entry($this->entry, '/Entry #1'))->username());
}

#[@test]
public function password() {
$this->assertEquals(new ProtectedValue("\323\$c", "\274J\006"), (new Entry($this->entry, '/Entry #1'))->password());
}

#[@test]
public function icon_field() {
$this->assertEquals('42', (new Entry($this->entry, '/Entry #1'))->field('IconID'));
}
}

55 changes: 55 additions & 0 deletions src/test/php/info/keepass/unittest/GroupTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php namespace info\keepass\unittest;

use info\keepass\Group;
use util\UUID;

class GroupTest extends \unittest\TestCase {
private $group;

/** @return void */
public function setUp() {
$this->group= [
'UUID' => 'Vy28plCgw0u5Y0PtP2c/2Q==',
'Name' => 'Test',
'Notes' => 'Notes',
'IconID' => '48',
'Times' => '',
'IsExpanded' => 'True',
'DefaultAutoTypeSequence' => null,
'EnableAutoType' => 'null',
'EnableSearching' => 'null',
'LastTopVisibleEntry' => 'AAAAAAAAAAAAAAAAAAAAAA==',
];
}

#[@test]
public function can_create() {
new Group($this->group, '/Test');
}

#[@test]
public function path() {
$this->assertEquals('/Test', (new Group($this->group, '/Test'))->path());
}

#[@test]
public function uuid() {
$this->assertEquals(new UUID('572dbca6-50a0-c34b-b963-43ed3f673fd9'), (new Group($this->group, '/Test'))->uuid());
}

#[@test]
public function name() {
$this->assertEquals('Test', (new Group($this->group, '/Test'))->name());
}

#[@test]
public function notes() {
$this->assertEquals('Notes', (new Group($this->group, '/Test'))->notes());
}

#[@test]
public function icon_field() {
$this->assertEquals('48', (new Group($this->group, '/Test'))->field('IconID'));
}
}

0 comments on commit eed6f62

Please sign in to comment.