There are wrong condition in removeGranularity method, it doesnt work if I want to remove granularity with key '0' in granularity array.

Method code:

  public function removeGranularity($g) {
    if ($key = array_search($g, $this->granularity)) {
      unset($this->granularity[$key]);
    }
  }

Test case:

$this->granularity = array(
    0 => hour,
    1 => minute,
    2 => second
);

This method will not work If I want to remove 'hour' element:

  $this->removeGranularity('hour');

because key of 'hour' element is '0', and ($key = array_search($g, $this->granularity)) condition is false.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

skorzh’s picture

Status: Active » Needs review
FileSize
473 bytes

My little patch fixed it.
Test case:

$date = new DateObject(time());
dpm($date->granularity);

$date->removeGranularity('minute');
$date->removeGranularity('hour'); // It doesnt remove 'hour' element without patch

dpm($date->granularity);
skorzh’s picture

No updates here?

skorzh’s picture

Added more properly patch

Spleshka’s picture

Status: Needs review » Reviewed & tested by the community

Tested this patch, now it works perfectly, thanks!

podarok’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Patch looks good.
But we have to cover it by test.
Please, add some

skorzh’s picture

Status: Needs work » Needs review
FileSize
1.6 KB

Added tests, check please now

Spleshka’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

I like this test! So I believe now it is ready to be commited :-)

podarok’s picture

Status: Reviewed & tested by the community » Fixed

#7 merged
Thanks!

  • podarok committed 8c94acd on 7.x-2.x authored by korgik
    Issue #2359673 by korgik: Fixed Wrong condition in removeGranularity...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.