From fcfb2732756ce33de31982908197766b066a7acc Mon Sep 17 00:00:00 2001 From: monishdeb Date: Wed, 14 Oct 2015 21:41:13 +0530 Subject: [PATCH] Added test-case for Custom Search - Household and State --- .../Contact/Form/Search/Custom/GroupTest.php | 7 +- .../Contact/Form/Search/Custom/SampleTest.php | 253 ++++++++++++++++++ .../Search/Custom/SampleTestDataProvider.php | 124 +++++++++ .../group-dataset.xml} | 0 .../Search/Custom/datasets/sample-dataset.xml | 92 +++++++ 5 files changed, 473 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/Form/Search/Custom/SampleTest.php create mode 100644 tests/phpunit/CRM/Contact/Form/Search/Custom/SampleTestDataProvider.php rename tests/phpunit/CRM/Contact/Form/Search/Custom/{dataset.xml => datasets/group-dataset.xml} (100%) create mode 100644 tests/phpunit/CRM/Contact/Form/Search/Custom/datasets/sample-dataset.xml diff --git a/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTest.php b/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTest.php index c4855c7060..17c19c7d6c 100644 --- a/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTest.php +++ b/tests/phpunit/CRM/Contact/Form/Search/Custom/GroupTest.php @@ -97,11 +97,12 @@ class CRM_Contact_Form_Search_Custom_GroupTest extends CiviUnitTestCase { $op = new PHPUnit_Extensions_Database_Operation_Insert(); $op->execute($this->_dbconn, $this->createFlatXMLDataSet( - dirname(__FILE__) . '/dataset.xml' + dirname(__FILE__) . '/datasets/group-dataset.xml' ) ); $obj = new CRM_Contact_Form_Search_Custom_Group($fv); + $sql = $obj->all(); $dao = CRM_Core_DAO::executeQuery($sql); @@ -133,7 +134,7 @@ class CRM_Contact_Form_Search_Custom_GroupTest extends CiviUnitTestCase { $op = new PHPUnit_Extensions_Database_Operation_Insert(); $op->execute($this->_dbconn, $this->createFlatXMLDataSet( - dirname(__FILE__) . '/dataset.xml' + dirname(__FILE__) . '/datasets/group-dataset.xml' ) ); $obj = new CRM_Contact_Form_Search_Custom_Group($fv); @@ -169,7 +170,7 @@ class CRM_Contact_Form_Search_Custom_GroupTest extends CiviUnitTestCase { $op = new PHPUnit_Extensions_Database_Operation_Insert(); $op->execute($this->_dbconn, $this->createFlatXMLDataSet( - dirname(__FILE__) . '/dataset.xml' + dirname(__FILE__) . '/datasets/group-dataset.xml' ) ); $obj = new CRM_Contact_Form_Search_Custom_Group($fv); diff --git a/tests/phpunit/CRM/Contact/Form/Search/Custom/SampleTest.php b/tests/phpunit/CRM/Contact/Form/Search/Custom/SampleTest.php new file mode 100644 index 0000000000..0c66a4fa8d --- /dev/null +++ b/tests/phpunit/CRM/Contact/Form/Search/Custom/SampleTest.php @@ -0,0 +1,253 @@ +foreignKeyChecksOff(); + + $this->quickCleanup($this->_tablesToTruncate); + + // echo "testCount\n"; + $op = new PHPUnit_Extensions_Database_Operation_Insert(); + $op->execute($this->_dbconn, + $this->createFlatXMLDataSet( + dirname(__FILE__) . '/datasets/sample-dataset.xml' + ) + ); + + $obj = new CRM_Contact_Form_Search_Custom_Sample($fv); + + $this->assertEquals($count, $obj->count(), + 'In line ' . __LINE__ + ); + } + + /** + * Test CRM_Contact_Form_Search_Custom_Sample::all() + * @dataProvider dataProvider + * @param $fv + * @param $count + * @param $ids + * @param $full + * @throws \Exception + */ + public function testAll($fv, $count, $ids, $full) { + // Truncate affected tables + $this->quickCleanup($this->_tablesToTruncate); + + // echo "testAll\n"; + $op = new PHPUnit_Extensions_Database_Operation_Insert(); + $op->execute($this->_dbconn, + $this->createFlatXMLDataSet( + dirname(__FILE__) . '/datasets/sample-dataset.xml' + ) + ); + $obj = new CRM_Contact_Form_Search_Custom_Sample($fv); + $sql = $obj->all(); + $this->assertTrue(is_string($sql)); + $dao = CRM_Core_DAO::executeQuery($sql); + $all = array(); + while ($dao->fetch()) { + $all[] = array( + 'contact_id' => $dao->contact_id, + 'contact_type' => $dao->contact_type, + 'household_name' => $dao->sort_name, + ); + } + asort($all); + $this->assertEquals($full, $all); + } + + /** + * Test CRM_Contact_Form_Search_Custom_Sample::contactIDs() + * @dataProvider dataProvider + * @param $fv + * @param $count + * @param $ids + * @param $full + * @throws \Exception + */ + public function testContactIDs($fv, $count, $ids, $full) { + // Truncate affected tables + $this->quickCleanup($this->_tablesToTruncate); + + // echo "testContactIDs\n"; + $op = new PHPUnit_Extensions_Database_Operation_Insert(); + $op->execute($this->_dbconn, + $this->createFlatXMLDataSet( + dirname(__FILE__) . '/datasets/sample-dataset.xml' + ) + ); + $obj = new CRM_Contact_Form_Search_Custom_Sample($fv); + $sql = $obj->contactIDs(); + $this->assertTrue(is_string($sql)); + $dao = CRM_Core_DAO::executeQuery($sql); + $contacts = array(); + while ($dao->fetch()) { + $contacts[$dao->contact_id] = 1; + } + $contacts = array_keys($contacts); + sort($contacts, SORT_NUMERIC); + $this->assertEquals($ids, $contacts); + } + + /** + * Test CRM_Contact_Form_Search_Custom_Group::columns() + * It returns an array of translated name => keys + */ + public function testColumns() { + $formValues = array(); + $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues); + $columns = $obj->columns(); + $this->assertTrue(is_array($columns)); + foreach ($columns as $key => $value) { + $this->assertTrue(is_string($key)); + $this->assertTrue(is_string($value)); + } + } + + /** + * Test CRM_Contact_Form_Search_Custom_Group::from() + * @todo write this test + */ + public function SKIPPED_testFrom() { + } + + /** + * Test CRM_Contact_Form_Search_Custom_Group::summary() + * It returns NULL + */ + public function testSummary() { + $formValues = array(); + $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); + $this->assertNull($obj->summary()); + } + + /** + * Test CRM_Contact_Form_Search_Custom_Sample::templateFile() + * Returns the path to the file as a string + */ + public function testTemplateFile() { + $formValues = array(); + $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); + $fileName = $obj->templateFile(); + $this->assertTrue(is_string($fileName)); + //FIXME: we would need to search the include path to do the following + //$this->assertTrue( file_exists( $fileName ) ); + } + + /** + * Test CRM_Contact_Form_Search_Custom_Sample with saved_search_id + * With true argument it returns list of contact IDs + */ + public function testSavedSearch() { + $this->quickCleanup($this->_tablesToTruncate); + + $op = new PHPUnit_Extensions_Database_Operation_Insert(); + $op->execute($this->_dbconn, + $this->createFlatXMLDataSet( + dirname(__FILE__) . '/datasets/sample-dataset.xml' + ) + ); + + $dataset[1] = array('id' => array(12)); + $dataset[2] = array('id' => array(10, 11)); + + $ssdao = CRM_Core_DAO::executeQuery("SELECT * FROM civicrm_saved_search"); + while($ssdao->fetch()) { + $fv = CRM_Contact_BAO_SavedSearch::getFormValues($ssdao->id); + $obj = new CRM_Contact_Form_Search_Custom_Sample($fv); + $sql = $obj->contactIDs(); + $this->assertTrue(is_string($sql)); + $dao = CRM_Core_DAO::executeQuery($sql); + $contacts = array(); + while ($dao->fetch()) { + $contacts[] = $dao->contact_id; + } + sort($contacts, SORT_NUMERIC); + $this->assertEquals($dataset[$ssdao->id]['id'], $contacts); + } + } + +} diff --git a/tests/phpunit/CRM/Contact/Form/Search/Custom/SampleTestDataProvider.php b/tests/phpunit/CRM/Contact/Form/Search/Custom/SampleTestDataProvider.php new file mode 100644 index 0000000000..48075109e1 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Form/Search/Custom/SampleTestDataProvider.php @@ -0,0 +1,124 @@ + array('household_name' => 'Household 9'), + 'id' => array( + '9', + ), + ), + // Search by Household name: 'Household' + array( + 'fv' => array('household_name' => 'Household'), + 'id' => array( + '9', + '10', + '11', + '12', + ), + ), + // Search by State: California + array( + 'fv' => array('state_province_id' => '1004'), + 'id' => array( + '10', + '11', + ), + ), + // Search by State: New York + array( + 'fv' => array('state_province_id' => '1031'), + 'id' => array( + '12', + ), + ), + ); + + public function _construct() { + $this->i = 0; + } + + public function rewind() { + $this->i = 0; + } + + /** + * @return array + */ + public function current() { + $count = count($this->dataset[$this->i]['id']); + $ids = $this->dataset[$this->i]['id']; + $full = array(); + foreach ($this->dataset[$this->i]['id'] as $key => $value) { + $full[] = array( + 'contact_id' => $value, + 'contact_type' => 'Household', + 'household_name' => "Household $value", + ); + } + return array($this->dataset[$this->i]['fv'], $count, $ids, $full); + } + + /** + * @return int + */ + public function key() { + return $this->i; + } + + public function next() { + $this->i++; + } + + /** + * @return bool + */ + public function valid() { + return isset($this->dataset[$this->i]); + } + +} diff --git a/tests/phpunit/CRM/Contact/Form/Search/Custom/dataset.xml b/tests/phpunit/CRM/Contact/Form/Search/Custom/datasets/group-dataset.xml similarity index 100% rename from tests/phpunit/CRM/Contact/Form/Search/Custom/dataset.xml rename to tests/phpunit/CRM/Contact/Form/Search/Custom/datasets/group-dataset.xml diff --git a/tests/phpunit/CRM/Contact/Form/Search/Custom/datasets/sample-dataset.xml b/tests/phpunit/CRM/Contact/Form/Search/Custom/datasets/sample-dataset.xml new file mode 100644 index 0000000000..114272ef63 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Form/Search/Custom/datasets/sample-dataset.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + -- 2.25.1