Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix Zend\Mail\Headers::removeHeader is not removing every header matching header name #5937

Closed
wants to merge 1 commit into from

2 participants

@Xerkus

Zend\Mail\Headers::removeHeader should remove all headers matching field
name instead of just first match.

@Xerkus Xerkus Fix removeHeader not removing every applicable headers
Zend\Mail\Headers::removeHeader should remove all headers matching field
name instead of just first match.
6ce0a9d
@weierophinney weierophinney added this to the 2.3.0 milestone
@weierophinney weierophinney self-assigned this
@weierophinney

Merged to develop for release with 2.3.0.

@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-mail
@weierophinney weierophinney Merge pull request zendframework/zf2#5937 from Xerkus/hotfix/zend-mai…
…l-headers-removebyname

Fix Zend\Mail\Headers::removeHeader is not removing every header matching header name
a9a10db
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-mail
@weierophinney weierophinney Merge branch 'hotfix/5937' into develop 282c325
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 9, 2014
  1. @Xerkus

    Fix removeHeader not removing every applicable headers

    Xerkus authored
    Zend\Mail\Headers::removeHeader should remove all headers matching field
    name instead of just first match.
This page is out of date. Refresh to see the latest.
Showing with 40 additions and 9 deletions.
  1. +15 −7 library/Zend/Mail/Headers.php
  2. +25 −2 tests/ZendTest/Mail/HeadersTest.php
View
22 library/Zend/Mail/Headers.php
@@ -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;
}
View
27 tests/ZendTest/Mail/HeadersTest.php
@@ -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()
Something went wrong with that request. Please try again.