Skip to content

Commit

Permalink
Fix removeHeader not removing every applicable headers
Browse files Browse the repository at this point in the history
Zend\Mail\Headers::removeHeader should remove all headers matching field
name instead of just first match.
  • Loading branch information
Xerkus committed Mar 9, 2014
1 parent 5eae447 commit 6ce0a9d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
22 changes: 15 additions & 7 deletions library/Zend/Mail/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,26 @@ public function addHeader(Header\HeaderInterface $header)
/**
* Remove a Header from the container
*
* @param string $fieldName
* @param string|Header\HeaderInterface field name or specific header instance to remove
* @return bool
*/
public function removeHeader($fieldName)
public function removeHeader($instanceOrFieldName)
{
$key = $this->normalizeFieldName($fieldName);
$index = array_search($key, $this->headersKeys, true);
if ($index !== false) {
unset($this->headersKeys[$index]);
unset($this->headers[$index]);
if ($instanceOrFieldName instanceof Header\HeaderInterface) {
$indexes = array_keys($this->headers, $instanceOrFieldName, true);
} else {
$key = $this->normalizeFieldName($instanceOrFieldName);
$indexes = array_keys($this->headersKeys, $key, true);
}

if (!empty($indexes)) {
foreach ($indexes as $index) {
unset ($this->headersKeys[$index]);
unset ($this->headers[$index]);
}
return true;
}

return false;
}

Expand Down
27 changes: 25 additions & 2 deletions tests/ZendTest/Mail/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,34 @@ public function testHeadersCanRemoveHeader()
{
$headers = new Mail\Headers();
$headers->addHeaders(array('Foo' => 'bar', 'Baz' => 'baz'));
$header = $headers->get('foo');
$this->assertEquals(2, $headers->count());
$headers->removeHeader($header->getFieldName());
$headers->removeHeader('foo');
$this->assertEquals(1, $headers->count());
$this->assertFalse($headers->has('foo'));
$this->assertTrue($headers->has('baz'));
}

public function testRemoveHeaderWithFieldNameWillRemoveAllInstances()
{
$headers = new Mail\Headers();
$headers->addHeaders(array(array('Foo' => 'foo'), array('Foo' => 'bar'), 'Baz' => 'baz'));
$this->assertEquals(3, $headers->count());
$headers->removeHeader('foo');
$this->assertEquals(1, $headers->count());
$this->assertFalse($headers->get('foo'));
$this->assertTrue($headers->has('baz'));
}

public function testRemoveHeaderWithInstanceWillRemoveThatInstance()
{
$headers = new Mail\Headers();
$headers->addHeaders(array(array('Foo' => 'foo'), array('Foo' => 'bar'), 'Baz' => 'baz'));
$header = $headers->get('foo')->current();
$this->assertEquals(3, $headers->count());
$headers->removeHeader($header);
$this->assertEquals(2, $headers->count());
$this->assertTrue($headers->has('foo'));
$this->assertNotSame($header, $headers->get('foo'));
}

public function testHeadersCanClearAllHeaders()
Expand Down

0 comments on commit 6ce0a9d

Please sign in to comment.