From ab723d18af4ea6cd3dc32b1f9084a0bbe401701a Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 9 Jul 2018 17:47:08 -0700 Subject: [PATCH] (dev/core#217) Add E2E_Core_PrevNextTest --- tests/phpunit/E2E/Core/PrevNextTest.php | 249 ++++++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 tests/phpunit/E2E/Core/PrevNextTest.php diff --git a/tests/phpunit/E2E/Core/PrevNextTest.php b/tests/phpunit/E2E/Core/PrevNextTest.php new file mode 100644 index 0000000000..b5e17d838a --- /dev/null +++ b/tests/phpunit/E2E/Core/PrevNextTest.php @@ -0,0 +1,249 @@ +prevNext = \Civi::service('prevnext'); + $this->cacheKey = 'PrevNextTest_' . \CRM_Utils_String::createRandom(16, \CRM_Utils_String::ALPHANUMERIC); + $this->assertTrue( + \CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_contact') > 25, + 'The contact table must have at least 25 records.' + ); + } + + protected function tearDown() { + \Civi::service('prevnext')->deleteItem(NULL, $this->cacheKey); + } + + public function testFillSql() { + $start = 0; + $prefillLimit = 25; + $sort = NULL; + + $query = new \CRM_Contact_BAO_Query(array(), NULL, NULL, FALSE, FALSE, 1, FALSE, TRUE, FALSE, NULL, 'AND'); + $sql = $query->searchQuery($start, $prefillLimit, $sort, FALSE, $query->_includeContactIds, + FALSE, TRUE, TRUE); + $selectSQL = "SELECT DISTINCT '$this->cacheKey', contact_a.id, contact_a.sort_name"; + $sql = str_replace(array("SELECT contact_a.id as contact_id", "SELECT contact_a.id as id"), $selectSQL, $sql); + + $this->assertTrue( + $this->prevNext->fillWithSql($this->cacheKey, $sql), + "fillWithSql should return TRUE on success" + ); + + $this->assertEquals(25, $this->prevNext->getCount($this->cacheKey)); + $this->assertEquals(0, $this->prevNext->getCount('not-a-key-' . $this->cacheKey)); + + $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; + $this->assertCount($prefillLimit, $all); + $this->assertCount($prefillLimit, array_unique(array_keys($all))); + $this->assertEquals([1], array_unique(array_values($all))); + + $this->assertSelections([]); + } + + public function testFillArray() { + $rowSetA = [ + ['entity_id1' => 100, 'data' => 'Alice'], + ['entity_id1' => 400, 'data' => 'Bob'], + ['entity_id1' => 200, 'data' => 'Carol'], + ]; + $rowSetB = [ + ['entity_id1' => 300, 'data' => 'Dave'], + ]; + + $this->assertTrue( + $this->prevNext->fillWithArray($this->cacheKey, $rowSetA), + "fillWithArray should return TRUE on success" + ); + $this->assertTrue( + $this->prevNext->fillWithArray($this->cacheKey, $rowSetB), + "fillWithArray should return TRUE on success" + ); + + $this->assertEquals(4, $this->prevNext->getCount($this->cacheKey)); + $this->assertEquals(0, $this->prevNext->getCount('not-a-key-' . $this->cacheKey)); + + $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; + $this->assertEquals([100, 400, 200, 300], array_keys($all)); + $this->assertEquals([1], array_unique(array_values($all))); + + $this->assertSelections([]); + } + + public function getFillFunctions() { + return [ + ['testFillSql'], + ['testFillArray'], + ]; + } + + /** + * Select and unselect one item. + * + * @dataProvider getFillFunctions + */ + public function testMarkSelection_1($fillFunction) { + call_user_func([$this, $fillFunction]); + + $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; + list ($id1, $id2) = array_keys($all); + $this->prevNext->markSelection($this->cacheKey, 'select', $id1); + + $this->assertSelections([$id1]); + + $this->prevNext->markSelection($this->cacheKey, 'unselect', $id1); + $this->assertSelections([]); + } + + /** + * Select and unselect two items. + * + * @dataProvider getFillFunctions + */ + public function testMarkSelection_2($fillFunction) { + call_user_func([$this, $fillFunction]); + + $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; + list ($id1, $id2, $id3) = array_keys($all); + + $this->prevNext->markSelection($this->cacheKey, 'select', [$id1, $id3]); + $this->assertSelections([$id1, $id3]); + + $this->prevNext->markSelection($this->cacheKey, 'unselect', $id1); + $this->assertSelections([$id3]); + + $this->prevNext->markSelection($this->cacheKey, 'select', $id2); + $this->assertSelections([$id2, $id3]); + + $this->prevNext->markSelection($this->cacheKey, 'unselect'); + $this->assertSelections([]); + } + + /** + * Check the neighbors of the first item. + * + * @dataProvider getFillFunctions + */ + public function testGetPosition_first($fillFunction) { + call_user_func([$this, $fillFunction]); + + $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; + list ($id1, $id2, $id3) = array_keys($all); + + $pos = $this->prevNext->getPositions($this->cacheKey, $id1); + + $this->assertTrue((bool) $pos['foundEntry']); + + $this->assertEquals($id2, $pos['next']['id1']); + $this->assertTrue(!empty($pos['next']['data'])); + + $this->assertTrue(!isset($pos['prev'])); + } + + /** + * Check the neighbors of a middle item. + * + * @dataProvider getFillFunctions + */ + public function testGetPosition_middle($fillFunction) { + call_user_func([$this, $fillFunction]); + + $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; + list ($id1, $id2, $id3) = array_keys($all); + + $pos = $this->prevNext->getPositions($this->cacheKey, $id2); + $this->assertTrue((bool) $pos['foundEntry']); + + $this->assertEquals($id3, $pos['next']['id1']); + $this->assertTrue(!empty($pos['next']['data'])); + + $this->assertEquals($id1, $pos['prev']['id1']); + $this->assertTrue(!empty($pos['prev']['data'])); + } + + /** + * Check the neighbors of the last item. + * + * @dataProvider getFillFunctions + */ + public function testGetPosition_last($fillFunction) { + call_user_func([$this, $fillFunction]); + + $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; + list ($idLast, $idPrev) = array_reverse(array_keys($all)); + + $pos = $this->prevNext->getPositions($this->cacheKey, $idLast); + $this->assertTrue((bool) $pos['foundEntry']); + + $this->assertTrue(!isset($pos['next'])); + + $this->assertEquals($idPrev, $pos['prev']['id1']); + $this->assertTrue(!empty($pos['prev']['data'])); + } + + /** + * Check the neighbors of the last item. + * + * @dataProvider getFillFunctions + */ + public function testGetPosition_invalid($fillFunction) { + call_user_func([$this, $fillFunction]); + + $pos = $this->prevNext->getPositions($this->cacheKey, 99999999); + $this->assertFalse((bool) $pos['foundEntry']); + $this->assertTrue(!isset($pos['next'])); + $this->assertTrue(!isset($pos['prev'])); + } + + public function testDeleteByKey() { + $this->testFillArray(); + + $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; + $this->assertEquals([100, 400, 200, 300], array_keys($all)); + + list ($id1, $id2, $id3) = array_keys($all); + $this->prevNext->markSelection($this->cacheKey, 'select', [$id1, $id3]); + $this->assertSelections([$id1, $id3]); + + $this->prevNext->deleteItem(NULL, $this->cacheKey); + $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; + $this->assertEquals([], array_keys($all)); + $this->assertSelections([]); + } + + + /** + * Assert that the current cacheKey has a list of selected contact IDs. + * + * @param array $ids + * Contact IDs that should be selected. + */ + protected function assertSelections($ids) { + $selected = $this->prevNext->getSelection($this->cacheKey)[$this->cacheKey]; + $this->assertEquals($ids, array_keys($selected)); + $this->assertCount(count($ids), $selected); + } + +} -- 2.25.1