Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
heiglandreas committed Mar 6, 2017
2 parents fb7c9b7 + d742925 commit a3c51d0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.7.2 - TBD
## 2.8.0 - TBD

### Added

- [#53](https://github.com/zendframework/zend-ldap/pull/53) Adds addAttribute-method
to Ldap-class
- [#57](https://github.com/zendframework/zend-ldap/pull/57) adds support for new
coding-standards.

Expand Down
36 changes: 36 additions & 0 deletions src/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,42 @@ public function delete($dn, $recursively = false)
return $this;
}

/**
* Add one or more attributes to the specified dn
*
* @param string|Dn $dn
* @param array $attributes
* @param bool $allowEmptyAttributes
* @return Ldap Provides a fluid interface
* @throws LdapException
*/
public function addAttributes($dn, array $attributes, $allowEmptyAttributes = false)
{
// Safety-flap: Check whether there are empty arrays that would cause
// complete removal of entries without the emptyAll flag.
if ($allowEmptyAttributes !== true) {
foreach ($attributes as $key => $value) {
if (empty($value)) {
unset($attributes[$key]);
}
}
}

if ($dn instanceof Dn) {
$dn = $dn->toString();
}

ErrorHandler::start(E_WARNING);
$entryAdded = ldap_mod_add($this->resource, $dn, $attributes);
ErrorHandler::stop();

if ($entryAdded === false) {
throw new Exception\LdapException($this, 'adding attribute: ' . $dn);
}

return $this;
}

/**
* Delete single attributes from a LDAP-Node
*
Expand Down
36 changes: 36 additions & 0 deletions test/OfflineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,40 @@ public function testRemovingAttributesFails()
$ldap = new \Zend\Ldap\Ldap();
$ldap->deleteAttributes('foo', ['bar']);
}

/**
* @dataProvider removingAttributesProvider
*/
public function testAddingAttributes(
$dn,
$attributes,
$allowEmptyAttributes,
$expectedDn,
$expectedAttributesToRemove
) {
$ldap_mod_add = $this->getFunctionMock('Zend\\Ldap', "ldap_mod_add");
$ldap_mod_add->expects($this->once())
->with(
$this->isNull(),
$this->equalTo($expectedDn),
$this->equalTo($expectedAttributesToRemove)
)
->willReturn(true);

$ldap = new \Zend\Ldap\Ldap();
$this->assertSame($ldap, $ldap->addAttributes($dn, $attributes, $allowEmptyAttributes));
}

/**
* @expectedException \Zend\Ldap\Exception\LdapException
*/
public function testAddingAttributesFails()
{
$ldap_mod_del = $this->getFunctionMock('Zend\\Ldap', 'ldap_mod_add');
$ldap_mod_del->expects($this->once())
->willReturn(false);

$ldap = new \Zend\Ldap\Ldap();
$ldap->addAttributes('foo', ['bar']);
}
}

0 comments on commit a3c51d0

Please sign in to comment.