Skip to content

Commit acaf401

Browse files
author
Jan Steemann
committed
fixed issue arangodb#182: Set hidden attributes is not working properly
1 parent f428fb4 commit acaf401

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

lib/triagens/ArangoDb/Document.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ public function filterHiddenAttributes($attributes)
239239

240240
if (is_array($hiddenAttributes)) {
241241
foreach ($hiddenAttributes as $hiddenAttributeName) {
242-
if (!in_array($hiddenAttributeName, $attributes)) {
243-
unset ($attributes[$hiddenAttributeName]);
242+
if (isset($attributes[$hiddenAttributeName])) {
243+
unset($attributes[$hiddenAttributeName]);
244244
}
245245
}
246246
}

tests/DocumentExtendedTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,87 @@ public function testGetAll()
746746
$this->assertArrayHasKey('_id', $result);
747747
$this->assertArrayHasKey('_rev', $result);
748748
}
749+
750+
/**
751+
* test to set some attributes and get all attributes of the document through getAll()
752+
* Also testing to optionally get internal attributes _id and _rev
753+
*/
754+
public function testHiddenAttributesGetAll()
755+
{
756+
$documentHandler = $this->documentHandler;
757+
758+
$document = Document::createFromArray(
759+
array(
760+
'_key' => 'test1',
761+
'isActive' => true,
762+
'password' => 'secret',
763+
'name' => 'foo'
764+
)
765+
);
766+
$documentHandler->add($this->collection->getId(), $document);
767+
768+
$document = Document::createFromArray(
769+
array(
770+
'_key' => 'test2',
771+
'isActive' => false,
772+
'password' => 'secret',
773+
'name' => 'bar'
774+
)
775+
);
776+
$documentHandler->add($this->collection->getId(), $document);
777+
778+
779+
$document = $documentHandler->getById($this->collection->getId(), "test1");
780+
$document->setHiddenAttributes(array('password'));
781+
$result = $document->getAll();
782+
783+
$this->assertTrue($result['isActive']);
784+
$this->assertEquals('foo', $result['name']);
785+
$this->assertArrayNotHasKey('password', $result);
786+
787+
// test with even more hidden attributes
788+
$document = $documentHandler->getById($this->collection->getId(), "test1");
789+
$document->setHiddenAttributes(array('isActive', 'password', 'foobar'));
790+
$result = $document->getAll();
791+
792+
$this->assertArrayNotHasKey('isActive', $result);
793+
$this->assertEquals('foo', $result['name']);
794+
$this->assertArrayNotHasKey('password', $result);
795+
796+
// fetch again, without hidden fields now
797+
$document = $documentHandler->getById($this->collection->getId(), "test1");
798+
$result = $document->getAll();
799+
800+
$this->assertTrue($result['isActive']);
801+
$this->assertEquals('foo', $result['name']);
802+
$this->assertEquals('secret', $result['password']);
803+
804+
805+
$document = $documentHandler->getById($this->collection->getId(), "test2");
806+
$document->setHiddenAttributes(array('password'));
807+
$result = $document->getAll();
808+
809+
$this->assertFalse($result['isActive']);
810+
$this->assertEquals('bar', $result['name']);
811+
$this->assertArrayNotHasKey('password', $result);
812+
813+
// test with even more hidden attributes
814+
$document = $documentHandler->getById($this->collection->getId(), "test2");
815+
$document->setHiddenAttributes(array('isActive', 'password', 'foobar'));
816+
$result = $document->getAll();
817+
818+
$this->assertArrayNotHasKey('isActive', $result);
819+
$this->assertEquals('bar', $result['name']);
820+
$this->assertArrayNotHasKey('password', $result);
821+
822+
// fetch again, without hidden fields now
823+
$document = $documentHandler->getById($this->collection->getId(), "test2");
824+
$result = $document->getAll();
825+
826+
$this->assertFalse($result['isActive']);
827+
$this->assertEquals('bar', $result['name']);
828+
$this->assertEquals('secret', $result['password']);
829+
}
749830

750831

751832
/**

0 commit comments

Comments
 (0)