From 8e3b2970429e5d38428361ca298bf53713f6aeb8 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 13 Sep 2021 17:06:06 -0700 Subject: [PATCH] (REF-2) Convert WorkflowMessage examples to use ExampleData classes --- .../CaseActivity/CaseAdhocExample.ex.php | 49 ++++++++++++ .../CaseActivity/CaseModelExample.ex.php | 49 ++++++++++++ .../CaseActivity/adhoc_1.ex.php | 32 -------- .../CaseActivity/class_1.ex.php | 33 -------- Civi/Test/WorkflowMessageTestTrait.php | 4 +- .../{alex.ex.php => Alex.ex.php} | 29 +++++-- .../WorkflowMessageExample.php | 79 +++++++++++++++++++ .../Case/WorkflowMessage/CaseActivityTest.php | 17 ++-- .../phpunit/api/v4/Entity/ExampleDataTest.php | 2 +- .../api/v4/Entity/WorkflowMessageTest.php | 12 +-- 10 files changed, 217 insertions(+), 89 deletions(-) create mode 100644 CRM/Case/WorkflowMessage/CaseActivity/CaseAdhocExample.ex.php create mode 100644 CRM/Case/WorkflowMessage/CaseActivity/CaseModelExample.ex.php delete mode 100644 CRM/Case/WorkflowMessage/CaseActivity/adhoc_1.ex.php delete mode 100644 CRM/Case/WorkflowMessage/CaseActivity/class_1.ex.php rename Civi/WorkflowMessage/GenericWorkflowMessage/{alex.ex.php => Alex.ex.php} (85%) create mode 100644 Civi/WorkflowMessage/WorkflowMessageExample.php diff --git a/CRM/Case/WorkflowMessage/CaseActivity/CaseAdhocExample.ex.php b/CRM/Case/WorkflowMessage/CaseActivity/CaseAdhocExample.ex.php new file mode 100644 index 0000000000..e05b2590cc --- /dev/null +++ b/CRM/Case/WorkflowMessage/CaseActivity/CaseAdhocExample.ex.php @@ -0,0 +1,49 @@ + "workflow/{$this->wfName}/{$this->exName}", + 'title' => ts('Case Activity (Adhoc-style example)'), + 'tags' => [], + ]; + } + + /** + * @inheritDoc + */ + public function build(array &$example): void { + $alex = \Civi\Test::example('workflow/generic/Alex'); + $contact = $this->extend($alex['data']['modelProps']['contact'], [ + 'role' => 'myrole', + ]); + $example['data'] = [ + 'workflow' => $this->wfName, + 'tokenContext' => [ + 'contact' => $contact, + ], + 'tplParams' => [ + 'contact' => $contact, + 'isCaseActivity' => 1, + 'client_id' => 101, + 'activityTypeName' => 'Follow up', + 'activitySubject' => 'Test 123', + 'idHash' => 'abcdefg', + 'activity' => [ + 'fields' => [ + [ + 'label' => 'Case ID', + 'type' => 'String', + 'value' => '1234', + ], + ], + ], + ], + ]; + } + +} diff --git a/CRM/Case/WorkflowMessage/CaseActivity/CaseModelExample.ex.php b/CRM/Case/WorkflowMessage/CaseActivity/CaseModelExample.ex.php new file mode 100644 index 0000000000..b661afa077 --- /dev/null +++ b/CRM/Case/WorkflowMessage/CaseActivity/CaseModelExample.ex.php @@ -0,0 +1,49 @@ + "workflow/{$this->wfName}/{$this->exName}", + 'title' => ts('Case Activity (Class-style example)'), + 'tags' => ['phpunit', 'preview'], + ]; + } + + /** + * @inheritDoc + */ + public function build(array &$example): void { + $alex = \Civi\Test::example('workflow/generic/Alex'); + $example['data'] = $this->extend($alex['data'], [ + 'workflow' => $this->wfName, + 'modelProps' => [ + 'contact' => [ + 'role' => 'myrole', + ], + 'isCaseActivity' => 1, + 'clientId' => 101, + 'activityTypeName' => 'Follow up', + 'activityFields' => [ + [ + 'label' => 'Case ID', + 'type' => 'String', + 'value' => '1234', + ], + ], + 'activitySubject' => 'Test 123', + 'activityCustomGroups' => [], + 'idHash' => 'abcdefg', + ], + ]); + $example['asserts'] = [ + 'default' => [ + ['for' => 'subject', 'regex' => '/\[case #abcdefg\] Test 123/'], + ['for' => 'text', 'regex' => '/Your Case Role\(s\) : myrole/'], + ], + ]; + } + +} diff --git a/CRM/Case/WorkflowMessage/CaseActivity/adhoc_1.ex.php b/CRM/Case/WorkflowMessage/CaseActivity/adhoc_1.ex.php deleted file mode 100644 index 47c9900e4d..0000000000 --- a/CRM/Case/WorkflowMessage/CaseActivity/adhoc_1.ex.php +++ /dev/null @@ -1,32 +0,0 @@ - ts('Case Activity (Adhoc-style example)'), - 'tags' => [], - 'data' => function (\Civi\WorkflowMessage\Examples $examples) { - $contact = $examples->extend('generic.alex.data.modelProps.contact', [ - 'role' => 'myrole', - ]); - return [ - 'tokenContext' => [ - 'contact' => $contact, - ], - 'tplParams' => [ - 'contact' => $contact, - 'isCaseActivity' => 1, - 'client_id' => 101, - 'activityTypeName' => 'Follow up', - 'activitySubject' => 'Test 123', - 'idHash' => 'abcdefg', - 'activity' => [ - 'fields' => [ - [ - 'label' => 'Case ID', - 'type' => 'String', - 'value' => '1234', - ], - ], - ], - ], - ]; - }, -]; diff --git a/CRM/Case/WorkflowMessage/CaseActivity/class_1.ex.php b/CRM/Case/WorkflowMessage/CaseActivity/class_1.ex.php deleted file mode 100644 index 65f413ecf1..0000000000 --- a/CRM/Case/WorkflowMessage/CaseActivity/class_1.ex.php +++ /dev/null @@ -1,33 +0,0 @@ - ts('Case Activity (Class-style example)'), - 'tags' => ['phpunit', 'preview'], - 'data' => function (\Civi\WorkflowMessage\Examples $examples) { - return $examples->extend('generic.alex.data', [ - 'modelProps' => [ - 'contact' => [ - 'role' => 'myrole', - ], - 'isCaseActivity' => 1, - 'clientId' => 101, - 'activityTypeName' => 'Follow up', - 'activityFields' => [ - [ - 'label' => 'Case ID', - 'type' => 'String', - 'value' => '1234', - ], - ], - 'activitySubject' => 'Test 123', - 'activityCustomGroups' => [], - 'idHash' => 'abcdefg', - ], - ]); - }, - 'asserts' => [ - 'default' => [ - ['for' => 'subject', 'regex' => '/\[case #abcdefg\] Test 123/'], - ['for' => 'text', 'regex' => '/Your Case Role\(s\) : myrole/'], - ], - ], -]; diff --git a/Civi/Test/WorkflowMessageTestTrait.php b/Civi/Test/WorkflowMessageTestTrait.php index 71f3fe2968..1c4bd77805 100644 --- a/Civi/Test/WorkflowMessageTestTrait.php +++ b/Civi/Test/WorkflowMessageTestTrait.php @@ -28,8 +28,8 @@ trait WorkflowMessageTestTrait { */ protected function findExamples(): \Civi\Api4\Generic\AbstractGetAction { return \Civi\Api4\ExampleData::get(0) - ->setSelect(['name', 'title', 'workflow', 'tags', 'data', 'asserts']) - ->addWhere('workflow', '=', $this->getWorkflowName()) + ->setSelect(['name', 'title', 'tags', 'data', 'asserts']) + ->addWhere('name', 'LIKE', 'workflow/' . $this->getWorkflowName() . '/%') ->addWhere('tags', 'CONTAINS', 'phpunit'); } diff --git a/Civi/WorkflowMessage/GenericWorkflowMessage/alex.ex.php b/Civi/WorkflowMessage/GenericWorkflowMessage/Alex.ex.php similarity index 85% rename from Civi/WorkflowMessage/GenericWorkflowMessage/alex.ex.php rename to Civi/WorkflowMessage/GenericWorkflowMessage/Alex.ex.php index a2ccd32167..5a7ba7d437 100644 --- a/Civi/WorkflowMessage/GenericWorkflowMessage/alex.ex.php +++ b/Civi/WorkflowMessage/GenericWorkflowMessage/Alex.ex.php @@ -1,8 +1,24 @@ [], - 'data' => function(\Civi\WorkflowMessage\Examples $examples) { - return [ + +namespace Civi\WorkflowMessage\GenericWorkflowMessage; + +class Alex extends \Civi\WorkflowMessage\WorkflowMessageExample { + + /** + * @inheritDoc + */ + public function getExamples(): iterable { + yield [ + 'name' => "workflow/{$this->wfName}/{$this->exName}", + 'tags' => [], + ]; + } + + /** + * @inheritDoc + */ + public function build(array &$example): void { + $example['data'] = [ 'modelProps' => [ 'contact' => [ 'contact_id' => '100', @@ -75,5 +91,6 @@ return [ ], ], ]; - }, -]; + } + +} diff --git a/Civi/WorkflowMessage/WorkflowMessageExample.php b/Civi/WorkflowMessage/WorkflowMessageExample.php new file mode 100644 index 0000000000..899c0a9165 --- /dev/null +++ b/Civi/WorkflowMessage/WorkflowMessageExample.php @@ -0,0 +1,79 @@ +wfClass = $m[1]; + $this->wfName = array_search($m[1], \Civi\WorkflowMessage\WorkflowMessage::getWorkflowNameClassMap()); + $this->exName = $m[2]; + } + + /** + * Get an example, merge/extend it with more data, and return the extended + * variant. + * + * @param array $base + * Baseline data to build upon. + * @param array $overrides + * Additional data to recursively add. + * + * @return array + * The result of merging the original example with the $overrides. + */ + public function extend($base, $overrides = []) { + \CRM_Utils_Array::extend($base, $overrides); + return $base; + } + +} diff --git a/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTest.php b/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTest.php index 3ca0fa32f4..c9ec0c824e 100644 --- a/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTest.php +++ b/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTest.php @@ -23,12 +23,12 @@ class CRM_Case_WorkflowMessage_CaseActivityTest extends CiviUnitTestCase { public function testAdhocClassEquiv() { $examples = \Civi\Api4\ExampleData::get(0) ->setSelect(['name', 'data']) - ->addWhere('name', 'IN', ['case_activity.adhoc_1', 'case_activity.class_1']) + ->addWhere('name', 'IN', ['workflow/case_activity/CaseAdhocExample', 'workflow/case_activity/CaseModelExample']) ->execute() ->indexBy('name') ->column('data'); - $byAdhoc = Civi\WorkflowMessage\WorkflowMessage::create('case_activity', $examples['case_activity.adhoc_1']); - $byClass = new CRM_Case_WorkflowMessage_CaseActivity($examples['case_activity.class_1']); + $byAdhoc = Civi\WorkflowMessage\WorkflowMessage::create('case_activity', $examples['workflow/case_activity/CaseAdhocExample']); + $byClass = new CRM_Case_WorkflowMessage_CaseActivity($examples['workflow/case_activity/CaseModelExample']); $this->assertSameWorkflowMessage($byClass, $byAdhoc, 'Compare byClass and byAdhoc: '); } @@ -52,9 +52,8 @@ class CRM_Case_WorkflowMessage_CaseActivityTest extends CiviUnitTestCase { * @throws \Civi\API\Exception\UnauthorizedException */ public function testExampleGet() { - $file = \Civi::paths()->getPath('[civicrm.root]/CRM/Case/WorkflowMessage/CaseActivity/class_1.ex.php'); - $workflow = 'case_activity'; - $name = 'case_activity.class_1'; + $file = \Civi::paths()->getPath('[civicrm.root]/CRM/Case/WorkflowMessage/CaseActivity/CaseModelExample.ex.php'); + $name = 'workflow/case_activity/CaseModelExample'; $this->assertTrue(file_exists($file), "Expect find canary file ($file)"); @@ -62,16 +61,16 @@ class CRM_Case_WorkflowMessage_CaseActivityTest extends CiviUnitTestCase { ->addWhere('name', '=', $name) ->execute() ->single(); - $this->assertEquals($workflow, $get['workflow']); + $this->assertEquals($name, $get['name']); $this->assertTrue(!isset($get['data'])); $this->assertTrue(!isset($get['asserts'])); $get = \Civi\Api4\ExampleData::get() ->addWhere('name', '=', $name) - ->addSelect('workflow', 'data') + ->addSelect('data') ->execute() ->single(); - $this->assertEquals($workflow, $get['workflow']); + $this->assertEquals($name, $get['name']); $this->assertEquals(100, $get['data']['modelProps']['contact']['contact_id']); $this->assertEquals('myrole', $get['data']['modelProps']['contact']['role']); } diff --git a/tests/phpunit/api/v4/Entity/ExampleDataTest.php b/tests/phpunit/api/v4/Entity/ExampleDataTest.php index 88ace338ff..8f71f57240 100644 --- a/tests/phpunit/api/v4/Entity/ExampleDataTest.php +++ b/tests/phpunit/api/v4/Entity/ExampleDataTest.php @@ -34,7 +34,7 @@ class ExampleDataTest extends UnitTestCase { */ public function testGet() { $file = \Civi::paths()->getPath('[civicrm.root]/Civi/WorkflowMessage/GenericWorkflowMessage/Alex.ex.php'); - $name = 'workflow/generic/alex'; + $name = 'workflow/generic/Alex'; $this->assertTrue(file_exists($file), "Expect find canary file ($file)"); diff --git a/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php b/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php index 0707c13dda..43cef7085b 100644 --- a/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php +++ b/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php @@ -37,10 +37,10 @@ class WorkflowMessageTest extends UnitTestCase { public function testRenderDefaultTemplate() { $ex = \Civi\Api4\ExampleData::get(0) - ->addWhere('name', '=', 'case_activity.class_1') - ->addSelect('data', 'workflow') + ->addWhere('name', '=', 'workflow/case_activity/CaseModelExample') + ->addSelect('data') ->addChain('render', WorkflowMessage::render() - ->setWorkflow('$workflow') + ->setWorkflow('$data.workflow') ->setValues('$data.modelProps')) ->execute() ->single(); @@ -50,7 +50,7 @@ class WorkflowMessageTest extends UnitTestCase { public function testRenderCustomTemplate() { $ex = \Civi\Api4\ExampleData::get(0) - ->addWhere('name', '=', 'case_activity.class_1') + ->addWhere('name', '=', 'workflow/case_activity/CaseModelExample') ->addSelect('data') ->execute() ->single(); @@ -68,14 +68,14 @@ class WorkflowMessageTest extends UnitTestCase { public function testRenderExamples() { $examples = \Civi\Api4\ExampleData::get(0) ->addWhere('tags', 'CONTAINS', 'phpunit') - ->addSelect('name', 'workflow', 'data', 'asserts') + ->addSelect('name', 'data', 'asserts') ->execute(); $this->assertTrue($examples->rowCount >= 1); foreach ($examples as $example) { $this->assertTrue(!empty($example['data']['modelProps']), sprintf("Example (%s) is tagged phpunit. It should have modelProps.", $example['name'])); $this->assertTrue(!empty($example['asserts']['default']), sprintf("Example (%s) is tagged phpunit. It should have assertions.", $example['name'])); $result = \Civi\Api4\WorkflowMessage::render(0) - ->setWorkflow($example['workflow']) + ->setWorkflow($example['data']['workflow']) ->setValues($example['data']['modelProps']) ->execute() ->single(); -- 2.25.1