From 28536b048afd1594e445237484ed9b65d069979f Mon Sep 17 00:00:00 2001 From: John Kirk Date: Tue, 11 Apr 2017 14:23:20 +0000 Subject: [PATCH 1/5] Added Owner Id to Resource Config Option in the Upgrader, and the DAO. --- CRM/Booking/DAO/ResourceConfigOption.php | 10 ++++++++++ CRM/Booking/Upgrader.php | 6 +++++- sql/upgrade_1510.sql | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 sql/upgrade_1510.sql diff --git a/CRM/Booking/DAO/ResourceConfigOption.php b/CRM/Booking/DAO/ResourceConfigOption.php index bd33457c..4b3afec8 100644 --- a/CRM/Booking/DAO/ResourceConfigOption.php +++ b/CRM/Booking/DAO/ResourceConfigOption.php @@ -243,6 +243,16 @@ static function &fields() 'dataPattern' => '', 'export' => true, ) , + 'owner_id' => array( + 'name' => 'owner_id', + 'type' => CRM_Utils_Type::T_INT, + 'title' => ts('Owner Id') , + 'import' => true, + 'where' => 'civicrm_booking_resource_config_option.owner_id', + 'headerPattern' => '', + 'dataPattern' => '', + 'export' => true, + ) , ); } return self::$_fields; diff --git a/CRM/Booking/Upgrader.php b/CRM/Booking/Upgrader.php index ee1e7eb4..7e2918a4 100755 --- a/CRM/Booking/Upgrader.php +++ b/CRM/Booking/Upgrader.php @@ -119,7 +119,11 @@ public function upgrade_1100() { return TRUE; } - + public function upgrade_1510() { + $this->ctx->log->info('Applying update 1510'); + $this->executeSqlFile('sql/upgrade_1510.sql'); + return TRUE; + } /** * Example: Run a couple simple queries diff --git a/sql/upgrade_1510.sql b/sql/upgrade_1510.sql new file mode 100644 index 00000000..062d7e64 --- /dev/null +++ b/sql/upgrade_1510.sql @@ -0,0 +1,5 @@ +ALTER TABLE `civicrm_booking_resource_config_option` +ADD `owner_id` int(10) unsigned +DEFAULT NULL +COMMENT 'Add an owner id for resources of type contact.' +AFTER `is_deleted` From cd92e31b5638e2e634585289b41a0caaf77acb16 Mon Sep 17 00:00:00 2001 From: John Kirk Date: Tue, 11 Apr 2017 14:28:49 +0000 Subject: [PATCH 2/5] Added owner to Resource Config Option pages and forms. --- CRM/Admin/Form/ResourceConfigOption.php | 1 + CRM/Admin/Page/ResourceConfigOption.php | 11 +++++++++++ templates/CRM/Admin/Form/ResourceConfigOption.tpl | 5 ++++- templates/CRM/Admin/Page/ResourceConfigOption.tpl | 2 ++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CRM/Admin/Form/ResourceConfigOption.php b/CRM/Admin/Form/ResourceConfigOption.php index 16d1a31b..8a8c53f3 100644 --- a/CRM/Admin/Form/ResourceConfigOption.php +++ b/CRM/Admin/Form/ResourceConfigOption.php @@ -66,6 +66,7 @@ public function buildQuickForm($check = FALSE) { $this->add('text', 'label', ts('Label'), array('size' => 50, 'maxlength' => 255), TRUE); $this->add('text', 'price', ts('Price'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'price '), TRUE); $this->add('text', 'max_size', ts('Max Size'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'max_size '), TRUE); + $this->addEntityRef('owner_id', ts('Select Contact')); $this->add('text', 'weight', ts('Weight'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'weight'), TRUE); $this->add('checkbox', 'is_active', ts('Enabled?')); diff --git a/CRM/Admin/Page/ResourceConfigOption.php b/CRM/Admin/Page/ResourceConfigOption.php index 5d97e386..a9d13a02 100644 --- a/CRM/Admin/Page/ResourceConfigOption.php +++ b/CRM/Admin/Page/ResourceConfigOption.php @@ -144,6 +144,17 @@ function browse($action = NULL) { CRM_Core_DAO::storeValues($dao, $configOptions[$dao->id]); $configOptions[$dao->id]['unit'] = CRM_Utils_Array::value(CRM_Utils_Array::value('unit_id', $configOptions[$dao->id]), $units); + if ($dao->owner_id > 0) { + $configOptions[$dao->id]['owner'] = civicrm_api3('Contact', 'getvalue', array( + 'sequential' => 1, + 'return' => "display_name", + 'id' => $dao->owner_id, + )); + } + else { + $configOptions[$dao->id]['owner'] = ''; + } + // form all action links $action = array_sum(array_keys($this->links())); diff --git a/templates/CRM/Admin/Form/ResourceConfigOption.tpl b/templates/CRM/Admin/Form/ResourceConfigOption.tpl index b3657210..235a0a3d 100644 --- a/templates/CRM/Admin/Form/ResourceConfigOption.tpl +++ b/templates/CRM/Admin/Form/ResourceConfigOption.tpl @@ -48,7 +48,10 @@ {$form.unit_id.html} - + + {$form.owner_id.label}{$form.owner_id.html} + + {$form.weight.label}{$form.weight.html} diff --git a/templates/CRM/Admin/Page/ResourceConfigOption.tpl b/templates/CRM/Admin/Page/ResourceConfigOption.tpl index daa72618..de86ea2b 100644 --- a/templates/CRM/Admin/Page/ResourceConfigOption.tpl +++ b/templates/CRM/Admin/Page/ResourceConfigOption.tpl @@ -37,6 +37,7 @@ {ts}Price{/ts} {ts}Max Size{/ts} {ts}Unit{/ts} + {ts}Owner{/ts} {ts}Weight{/ts} {ts}Enabled?{/ts} @@ -47,6 +48,7 @@ {$row.price} {$row.max_size} {$row.unit} + {$row.owner} {$row.weight} {if $row.is_active eq 1} {ts}Yes{/ts} {else} {ts}No{/ts} {/if} {$row.action|replace:'xx':$row.id} From 5ab12a93ce2705fa35ed2e37cec03ae82390281d Mon Sep 17 00:00:00 2001 From: John Kirk Date: Tue, 11 Apr 2017 14:30:37 +0000 Subject: [PATCH 3/5] Corrected a spelling error. --- templates/CRM/Admin/Page/ResourceConfigOption.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/CRM/Admin/Page/ResourceConfigOption.tpl b/templates/CRM/Admin/Page/ResourceConfigOption.tpl index de86ea2b..063cc9c4 100644 --- a/templates/CRM/Admin/Page/ResourceConfigOption.tpl +++ b/templates/CRM/Admin/Page/ResourceConfigOption.tpl @@ -69,7 +69,7 @@ {ts}There are no resources configuration option.{/ts} {/if} {/if} From f0cf52e5e6d74d5b9656cacafcfe58c35cf5d924 Mon Sep 17 00:00:00 2001 From: John Kirk Date: Thu, 13 Apr 2017 14:20:11 +0000 Subject: [PATCH 4/5] Updated merge hook and added unit test that proves it works. --- booking.php | 36 +++++++----- phpunit.xml.dist | 18 ++++++ tests/phpunit/CRM/Booking/HookTest.php | 78 ++++++++++++++++++++++++++ tests/phpunit/bootstrap.php | 49 ++++++++++++++++ 4 files changed, 168 insertions(+), 13 deletions(-) create mode 100644 phpunit.xml.dist create mode 100644 tests/phpunit/CRM/Booking/HookTest.php create mode 100644 tests/phpunit/bootstrap.php diff --git a/booking.php b/booking.php index fa718f34..c5fc04f4 100644 --- a/booking.php +++ b/booking.php @@ -188,23 +188,33 @@ function booking_civicrm_entityTypes(&$entityTypes) { * Implementation of hook_civicrm_merge */ function booking_civicrm_merge ( $type, &$data, $mainId = NULL, $otherId = NULL, $tables = NULL ){ -if (!empty($mainId) && !empty($otherId) && $type == 'sqls'){ + if (!empty($mainId) && !empty($otherId)){ + if ($type == 'sqls') { + $query1 = " + UPDATE civicrm_booking + SET primary_contact_id=$mainId + WHERE primary_contact_id=$otherId; + "; + $query2 = " + UPDATE civicrm_booking + SET secondary_contact_id=$mainId + WHERE secondary_contact_id=$otherId; + "; + + require_once('CRM/Core/DAO.php'); + $dao = CRM_Core_DAO::executeQuery( $query1 ); + $dao = CRM_Core_DAO::executeQuery( $query2 ); + } - $query1 = " - UPDATE civicrm_booking - SET primary_contact_id=$mainId - WHERE primary_contact_id=$otherId; - "; - $query2 = " - UPDATE civicrm_booking - SET secondary_contact_id=$mainId - WHERE secondary_contact_id=$otherId; + $updateResourceOwnerQuery = " + UPDATE civicrm_booking_resource_config_option + SET owner_id=$mainId + WHERE owner_id=$otherId; "; - require_once('CRM/Core/DAO.php'); - $dao = CRM_Core_DAO::executeQuery( $query1 ); - $dao = CRM_Core_DAO::executeQuery( $query2 ); + $dao = CRM_Core_DAO::executeQuery( $updateResourceOwnerQuery ); + echo "merge hook thrown\n"; } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 00000000..0f9f25d3 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,18 @@ + + + + + ./tests/phpunit + + + + + ./ + + + + + + + + diff --git a/tests/phpunit/CRM/Booking/HookTest.php b/tests/phpunit/CRM/Booking/HookTest.php new file mode 100644 index 00000000..2b66fd91 --- /dev/null +++ b/tests/phpunit/CRM/Booking/HookTest.php @@ -0,0 +1,78 @@ +installMe(__DIR__) + ->sqlFile(__DIR__ . '/../../../../sql/upgrade_1510.sql') + ->apply(); + } + + public function setUp() { + parent::setUp(); + } + + public function tearDown() { + parent::tearDown(); + } + + public function testMergeResourceOwner() { + $mainOwner = civicrm_api3('Contact', 'create', array( + 'contact_type' => 'organization', + 'organization_name' => 'Main Owner', + )); + + $otherOwner = civicrm_api3('Contact', 'create', array( + 'contact_type' => 'organization', + 'organization_name' => 'Other Owner', + )); + + $resourceConfigSet = civicrm_api3('ResourceConfigSet', 'create', array( + 'sequential' => 1, + 'title' => "test resource config set", + 'weight' => 1, + )); + + civicrm_api3('ResourceConfigOption', 'create', array( + 'sequential' => 1, + 'owner_id' => $otherOwner['id'], + 'set_id' => $resourceConfigSet['id'], + 'label' => "Test", + 'price' => "5.00", + 'weight' => 1, + )); + + $merger = new CRM_Dedupe_Merger(); + + $pairs = array(array('dstID' => $mainOwner['id'], 'srcID' => $otherOwner['id'])); + + $merger->merge($pairs); + + // Check that ownership of the resource config option has move to the main owner. + $this->assertEquals(1, civicrm_api3('ResourceConfigOption', 'getcount', array( + 'owner_id' => $mainOwner['id'], + ))); + } +} diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php new file mode 100644 index 00000000..9de4be63 --- /dev/null +++ b/tests/phpunit/bootstrap.php @@ -0,0 +1,49 @@ + array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR); + $oldOutput = getenv('CV_OUTPUT'); + putenv("CV_OUTPUT=json"); + $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__); + putenv("CV_OUTPUT=$oldOutput"); + fclose($pipes[0]); + $result = stream_get_contents($pipes[1]); + fclose($pipes[1]); + if (proc_close($process) !== 0) { + throw new RuntimeException("Command failed ($cmd):\n$result"); + } + switch ($decode) { + case 'raw': + return $result; + + case 'phpcode': + // If the last output is /*PHPCODE*/, then we managed to complete execution. + if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") { + throw new \RuntimeException("Command failed ($cmd):\n$result"); + } + return $result; + + case 'json': + return json_decode($result, 1); + + default: + throw new RuntimeException("Bad decoder format ($decode)"); + } +} From 68649d18983bede59324417d0597cd6377082d1b Mon Sep 17 00:00:00 2001 From: John Kirk Date: Thu, 13 Apr 2017 20:58:50 +0000 Subject: [PATCH 5/5] Minor tidying: including updating install sql. --- CRM/Admin/Form/ResourceConfigOption.php | 2 +- sql/civibooking_install.sql | 3 ++- tests/phpunit/CRM/Booking/HookTest.php | 3 +-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CRM/Admin/Form/ResourceConfigOption.php b/CRM/Admin/Form/ResourceConfigOption.php index 8a8c53f3..3d987030 100644 --- a/CRM/Admin/Form/ResourceConfigOption.php +++ b/CRM/Admin/Form/ResourceConfigOption.php @@ -66,7 +66,7 @@ public function buildQuickForm($check = FALSE) { $this->add('text', 'label', ts('Label'), array('size' => 50, 'maxlength' => 255), TRUE); $this->add('text', 'price', ts('Price'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'price '), TRUE); $this->add('text', 'max_size', ts('Max Size'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'max_size '), TRUE); - $this->addEntityRef('owner_id', ts('Select Contact')); + $this->addEntityRef('owner_id', ts('Select Resource Owner')); $this->add('text', 'weight', ts('Weight'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'weight'), TRUE); $this->add('checkbox', 'is_active', ts('Enabled?')); diff --git a/sql/civibooking_install.sql b/sql/civibooking_install.sql index 45fa3160..448e8f6b 100644 --- a/sql/civibooking_install.sql +++ b/sql/civibooking_install.sql @@ -238,7 +238,8 @@ CREATE TABLE `civicrm_booking_resource_config_option` ( `unit_id` varchar(512) COMMENT 'The unit associated with this config option. Implicit FK to option_value row in booking_size_unit option_group.', `weight` int unsigned NOT NULL , `is_active` tinyint DEFAULT 1 , - `is_deleted` tinyint DEFAULT 0 + `is_deleted` tinyint DEFAULT 0, + `owner_id` int(10) unsigned , PRIMARY KEY ( `id` ) diff --git a/tests/phpunit/CRM/Booking/HookTest.php b/tests/phpunit/CRM/Booking/HookTest.php index 2b66fd91..0e79ddde 100644 --- a/tests/phpunit/CRM/Booking/HookTest.php +++ b/tests/phpunit/CRM/Booking/HookTest.php @@ -26,7 +26,6 @@ public function setUpHeadless() { // See: https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md return \Civi\Test::headless() ->installMe(__DIR__) - ->sqlFile(__DIR__ . '/../../../../sql/upgrade_1510.sql') ->apply(); } @@ -68,7 +67,7 @@ public function testMergeResourceOwner() { $pairs = array(array('dstID' => $mainOwner['id'], 'srcID' => $otherOwner['id'])); - $merger->merge($pairs); + $merger::merge($pairs); // Check that ownership of the resource config option has move to the main owner. $this->assertEquals(1, civicrm_api3('ResourceConfigOption', 'getcount', array(