--- /dev/null
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7 |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015 |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM. |
+ | |
+ | CiviCRM is free software; you can copy, modify, and distribute it |
+ | under the terms of the GNU Affero General Public License |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
+ | |
+ | CiviCRM is distributed in the hope that it will be useful, but |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
+ | See the GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public |
+ | License and the CiviCRM Licensing Exception along |
+ | with this program; if not, contact CiviCRM LLC |
+ | at info[AT]civicrm[DOT]org. If you have questions about the |
+ | GNU Affero General Public License or the licensing of CiviCRM, |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Include parent class definition
+ */
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+/**
+ * Include class under test
+ */
+
+/**
+ * Include form definitions
+ */
+
+/**
+ * Include DAO to do queries
+ */
+
+/**
+ * Include dataProvider for tests
+ */
+
+/**
+ * Test contact custom search functions
+ *
+ * @package CiviCRM
+ */
+class CRM_Contact_Form_Search_Custom_SampleTest extends CiviUnitTestCase {
+ protected $_tablesToTruncate = array(
+ 'civicrm_address',
+ 'civicrm_saved_search',
+ 'civicrm_contact',
+ 'civicrm_option_value',
+ 'civicrm_option_group',
+ );
+
+ /**
+ * @return CRM_Contact_Form_Search_Custom_SamplTestDataProvider
+ */
+ public function dataProvider() {
+ return new CRM_Contact_Form_Search_Custom_SampleTestDataProvider();
+ }
+
+ public function setUp() {
+ parent::setUp();
+ }
+
+ public function tearDown() {
+ }
+
+ /**
+ * Test CRM_Contact_Form_Search_Custom_Sample::count()
+ * @dataProvider dataProvider
+ * @param $fv
+ * @param $count
+ * @param $ids
+ * @param $full
+ * @throws \Exception
+ */
+ public function testCount($fv, $count, $ids, $full) {
+ $this->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);
+ }
+ }
+
+}
--- /dev/null
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7 |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015 |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM. |
+ | |
+ | CiviCRM is free software; you can copy, modify, and distribute it |
+ | under the terms of the GNU Affero General Public License |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
+ | |
+ | CiviCRM is distributed in the hope that it will be useful, but |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
+ | See the GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public |
+ | License and the CiviCRM Licensing Exception along |
+ | with this program; if not, contact CiviCRM LLC |
+ | at info[AT]civicrm[DOT]org. If you have questions about the |
+ | GNU Affero General Public License or the licensing of CiviCRM, |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Provide data to the CRM_Contact_Form_Search_Custom_SampleTest class
+ *
+ * @package CiviCRM
+ */
+class CRM_Contact_Form_Search_Custom_SampleTestDataProvider implements Iterator {
+
+ /**
+ * @var integer
+ */
+ private $i = 0;
+
+ /**
+ * @var mixed[]
+ * This dataset describes various form values and what contact
+ * IDs should be selected when the form values are applied to the
+ * database in dataset.xml
+ */
+ private $dataset = array(
+ // Search by Household name: 'Household 9'
+ array(
+ 'fv' => 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]);
+ }
+
+}
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>
+<dataset>
+ <!-- State: none -->
+ <civicrm_contact
+ id="9"
+ contact_type="Household"
+ is_opt_out="0"
+ display_name="Household 9"
+ sort_name="Household 9"
+ household_name="Household 9"
+ />
+ <!-- State: California -->
+ <civicrm_contact
+ id="10"
+ contact_type="Household"
+ is_opt_out="0"
+ display_name="Household 10"
+ sort_name="Household 10"
+ household_name="Household 10"
+ />
+ <!-- State: California -->
+ <civicrm_contact
+ id="11"
+ contact_type="Household"
+ is_opt_out="0"
+ display_name="Household 11"
+ sort_name="Household 11"
+ household_name="Household 11"
+ />
+ <!-- State: New York -->
+ <civicrm_contact
+ id="12"
+ contact_type="Household"
+ is_opt_out="0"
+ display_name="Household 12"
+ sort_name="Household 12"
+ household_name="Household 12"
+ />
+ <!-- Save Search in quick value format -->
+ <!-- Household name: Household 12, State: New York -->
+ <civicrm_saved_search
+ id="1"
+ form_values='a:7:{i:0;a:5:{i:0;s:4:"csid";i:1;s:1:"=";i:2;s:1:"1";i:3;i:0;i:4;i:0;}i:2;a:5:{i:0;s:14:"household_name";i:1;s:1:"=";i:2;s:12:"Household 12";i:3;i:0;i:4;i:0;}i:3;a:5:{i:0;s:17:"state_province_id";i:1;s:1:"=";i:2;s:4:"1031";i:3;i:0;i:4;i:0;}i:4;a:5:{i:0;s:12:"toggleSelect";i:1;s:1:"=";i:2;s:1:"1";i:3;i:0;i:4;i:0;}i:5;a:5:{i:0;s:9:"mark_x_46";i:1;s:1:"=";i:2;s:1:"1";i:3;i:0;i:4;i:0;}i:6;a:5:{i:0;s:14:"customSearchID";i:1;s:1:"=";i:2;s:1:"1";i:3;i:0;i:4;i:0;}i:7;a:5:{i:0;s:17:"customSearchClass";i:1;s:1:"=";i:2;s:37:"CRM_Contact_Form_Search_Custom_Sample";i:3;i:0;i:4;i:0;}}'
+ />
+ <!-- Save Search in array(name=>value) format -->
+ <!-- State: California -->
+ <civicrm_saved_search
+ id="2"
+ form_values='a:8:{s:4:"csid";s:1:"1";s:5:"qfKey";s:37:"e195ef289c06af4f80197148086cc8db_3783";s:14:"household_name";s:0:"";s:17:"state_province_id";s:4:"1004";s:4:"task";s:2:"13";s:8:"radio_ts";s:6:"ts_all";s:12:"toggleSelect";s:1:"1";s:9:"mark_x_46";s:1:"1";}'
+ />
+ <civicrm_address
+ id="3"
+ contact_id="10"
+ location_type_id="1"
+ state_province_id="1004"
+ is_primary="1"
+ />
+ <civicrm_address
+ id="4"
+ contact_id="11"
+ location_type_id="1"
+ state_province_id="1004"
+ is_primary="1"
+ />
+ <civicrm_address
+ id="5"
+ contact_id="12"
+ location_type_id="1"
+ state_province_id="1031"
+ is_primary="1"
+ />
+ <civicrm_option_group
+ id="25"
+ name="custom_search"
+ description="Custom Search"
+ is_reserved="0"
+ is_active="1"
+ />
+ <civicrm_option_value
+ id="213"
+ option_group_id="25"
+ label="CRM_Contact_Form_Search_Custom_Sample"
+ value="1"
+ name="CRM_Contact_Form_Search_Custom_Sample"
+ filter="0"
+ weight="1"
+ description="Household Name and State"
+ is_optgroup="0"
+ is_reserved="0"
+ is_active="1"
+ />
+</dataset>