/** * Hydrate from a LDAP entry * * @param EntryInterface $entry Entry to use for loading * * @return void */ public function hydrateFromEntry(EntryInterface $entry) { $this->dn = $entry->getDn(); $this->attributes = array(); foreach ($entry->getAttributes() as $name => $data) { $attr = new NodeAttribute($name, null, $this->caseSensitive); $attr->add($data); $this->mergeAttribute($attr); } $this->snapshot(); }
/** * Tests retrieving raw node attributes for Ldap persistence * * @return void */ public function testGetRawAttributes() { $node = new Node(); $this->assertEquals(array(), $node->getRawAttributes(), 'No attributes set so an empty array is retrieved'); $node->mergeAttribute(new NodeAttribute('attr1')); $attr = new NodeAttribute('attr2'); $attr->add(array('value1', 'value2')); $node->mergeAttribute($attr); $attr = new NodeAttribute('attr3'); $attr->add('value3'); $node->mergeAttribute($attr); $this->assertEquals(array('attr1' => array(), 'attr2' => array('value1', 'value2'), 'attr3' => array('value3')), $node->getRawAttributes(), 'All combinations are parsed correctly'); }
/** * Tests attributes diff tracking * * @return void */ public function testDiffTracking() { $test = new NodeAttribute('test'); $test->add(array('value4', 'value5')); $test->snapshot(); $test->add(array('value1', 'value2', 'value3')); $test->remove(array('value4', 'value1', 'value5')); $test[] = 'value6'; $test[] = 'value4'; $test[] = 'value7'; unset($test[5]); //value6 $this->assertEquals(array('value2', 'value3', 'value7'), $test->getDiffAdditions(), 'Additions have been tracked'); $this->assertEquals(array('value5'), $test->getDiffDeletions(), 'Deletions have been tracked'); $this->assertEquals(array('value4'), $test->getDiffReplacements(), 'Replacements have been tracked'); $test->snapshot(); $this->assertEquals(array(), $test->getDiffAdditions(), 'Diff tracking has been reset'); $this->assertEquals(array(), $test->getDiffDeletions(), 'Diff tracking has been reset'); $this->assertEquals(array(), $test->getDiffReplacements(), 'Diff tracking has been reset'); }
/** * Tests saving existing nodes to the Ldap * * @return void */ public function testSaveExistingNodes() { $manager = new Manager($this->minimal, $this->driver); $manager->connect(); $manager->bind(); // Basic node update with search first $data = array('attr1' => array('value1'), 'attr2' => array('value1', 'value2'), 'attr3' => array('value3')); $node = $this->buildNode('test_dn', $data); $entry = new Entry('test_dn', array('attr' => array('value2'))); $this->driver->getConnection()->stackResults(array($entry)); $this->assertFalse($manager->save($node), 'Node persistence resulted in an update'); $this->assertSearchLog($this->driver->getConnection()->shiftLog(), 'test_dn', '(objectclass=*)', SearchInterface::SCOPE_BASE, null, array($entry)); $this->assertActionLog($this->driver->getConnection()->shiftLog(), 'attr_add', 'test_dn', $data); $this->assertNull($this->driver->getConnection()->shiftLog(), 'No other log'); $this->assertSnapshot($node, 'A node is snapshot after update'); try { $node->setDn('other'); $this->fail('Saving is like hydrating and so dn should be locked'); } catch (\InvalidArgumentException $e) { $this->assertRegExp('/Dn cannot be updated manually/', $e->getMessage()); } // Node updated again without underlying search $node->removeAttribute('attr1'); $node->get('attr2')->add('value4'); $node->get('attr2')->remove('value1'); $this->assertFalse($manager->save($node), 'Node has been marked hydrated so it is always an update - No need to feed a new entry'); $this->assertActionLog($this->driver->getConnection()->shiftLog(), 'attr_add', 'test_dn', array('attr2' => array('value4'))); $this->assertActionLog($this->driver->getConnection()->shiftLog(), 'attr_del', 'test_dn', array('attr1' => array(), 'attr2' => array('value1'))); $this->assertNull($this->driver->getConnection()->shiftLog(), 'No search performed, node was already hydrated'); $this->assertSnapshot($node, 'A node is snapshot after update'); // Support for all kinds of attributes manipulation $node->removeAttribute('attr2'); $attr = new NodeAttribute('attr2'); $attr->add(array('new1', 'new2')); $node->mergeAttribute($attr); $node->get('attr3')->add('new3'); $node->get('attr3')->remove('value3'); $this->assertFalse($manager->save($node), 'Node got updated'); $this->assertActionLog($this->driver->getConnection()->shiftLog(), 'attr_add', 'test_dn', array('attr3' => array('new3'))); $this->assertActionLog($this->driver->getConnection()->shiftLog(), 'attr_del', 'test_dn', array('attr3' => array('value3'))); $this->assertActionLog($this->driver->getConnection()->shiftLog(), 'attr_rep', 'test_dn', array('attr2' => array('new1', 'new2'))); $this->assertNull($this->driver->getConnection()->shiftLog(), 'No search performed, node was already hydrated'); $this->assertSnapshot($node, 'A node is snapshot after update'); }
/** * Node factory * * @param string $dn Distinguished name for the node * @param array $attributes Array of attributes * * @return Node node */ protected function buildNode($dn, $attributes) { $node = new Node(); $node->setDn($dn); foreach ($attributes as $name => $data) { $attr = new NodeAttribute($name); $attr->add($data); $node->mergeAttribute($attr); } return $node; }