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

Commit

Permalink
Merge pull request #2295 from fvovan/issue-2295
Browse files Browse the repository at this point in the history
CM_Paging_ModelAbstract cache misses
  • Loading branch information
fvovan authored Aug 22, 2016
2 parents 17baf31 + 3065a1a commit 37ffdbb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
17 changes: 8 additions & 9 deletions library/CM/Paging/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,21 @@ public function setFlattenItems($state) {
$this->_flattenItems = (bool) $state;
}


/**
* @return bool
*/
protected function _canContainUnprocessableItems() {
return ($this->_getStalenessChance() != 0);
}

protected function _clearItems() {
$this->_items = array();
$this->_itemsRaw = null;
$this->_itemsRawTree = null;
$this->_iteratorPosition = 0;
$this->_iteratorItems = null;
}

/**
* @return int Multiple of items per page to load from CM_PagingSource_Abstract
*/
Expand Down Expand Up @@ -381,14 +388,6 @@ private function _getItemOffset() {
return (int) $this->_pageOffset * $this->_pageSize;
}

private function _clearItems() {
$this->_items = array();
$this->_itemsRaw = null;
$this->_itemsRawTree = null;
$this->_iteratorPosition = 0;
$this->_iteratorItems = null;
}

private function _clearCount() {
$this->_count = null;
}
Expand Down
5 changes: 5 additions & 0 deletions library/CM/Paging/ModelAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public function _change() {
$this->_modelList = null;
}

protected function _clearItems() {
parent::_clearItems();
$this->_modelList = null;
}

/**
* @return int|null
*
Expand Down
29 changes: 29 additions & 0 deletions tests/library/CM/Paging/ModelAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,35 @@ public function testPagingVariableType() {
$this->assertContains('Array (', $ex->getMetaInfo()['itemRaw']);
}
}

public function testModelListInvalidation() {
CM_Config::get()->CM_Model_Abstract->types[CM_Paging_ModelAbstractTest_ModelMock::getTypeStatic()] = 'CM_Paging_ModelAbstractTest_ModelMock';
CM_Config::get()->CM_Model_Abstract->types[CM_Paging_ModelAbstractTest_ModelMock2::getTypeStatic()] = 'CM_Paging_ModelAbstractTest_ModelMock2';
$model1 = CM_Paging_ModelAbstractTest_ModelMock::create('foo');
$model2 = CM_Paging_ModelAbstractTest_ModelMock2::create('bar');
$model3 = CM_Paging_ModelAbstractTest_ModelMock::create('baz');
$model4 = CM_Paging_ModelAbstractTest_ModelMock::create('quux');
$model5 = CM_Paging_ModelAbstractTest_ModelMock::create('foobar');

$source = new CM_PagingSource_Array(array(
array('type' => $model1->getType(), 'id' => $model1->getId()),
array('type' => $model2->getType(), 'id' => $model2->getId()),
array('type' => $model3->getType(), 'id' => $model3->getId()),
array('type' => $model4->getType(), 'id' => $model4->getId()),
array('type' => $model5->getType(), 'id' => $model5->getId()),
array('type' => $model1->getType(), 'id' => 9999)
));
$modelPaging = $this->getMockBuilder('CM_Paging_ModelAbstract')->setConstructorArgs(array($source))
->getMockForAbstractClass();
/** @var CM_Paging_ModelAbstract $modelPaging */

$modelPaging->setPage(1, 2);
$this->assertEquals($model1, $modelPaging->getItem(0));
$this->assertEquals($model2, $modelPaging->getItem(1));
$modelPaging->setPage(2, 2);
$this->assertEquals($model3, $modelPaging->getItem(0));
$this->assertEquals($model4, $modelPaging->getItem(1));
}
}

class CM_Paging_ModelAbstractTest_ModelMock extends CM_Model_Abstract {
Expand Down

0 comments on commit 37ffdbb

Please sign in to comment.