4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 namespace api\v
4\Entity
;
21 use api\v
4\UnitTestCase
;
22 use Civi\Api4\OptionGroup
;
23 use Civi\Api4\OptionValue
;
24 use Civi\Test\TransactionalInterface
;
29 class OptionValueTest
extends UnitTestCase
implements TransactionalInterface
{
31 public function testNullDefault() {
32 OptionGroup
::create(FALSE)
33 ->addValue('name', 'myTestGroup')
34 ->addValue('title', 'myTestGroup')
37 $defaultId = OptionValue
::create()
38 ->addValue('option_group_id.name', 'myTestGroup')
39 ->addValue('label', 'One')
40 ->addValue('value', 1)
41 ->addValue('is_default', TRUE)
42 ->execute()->first()['id'];
44 $this->assertTrue(OptionValue
::get(FALSE)->addWhere('id', '=', $defaultId)->execute()->first()['is_default']);
46 // Now create a second option with is_default set to null.
47 // This should not interfere with the default setting in option one
49 ->addValue('option_group_id.name', 'myTestGroup')
50 ->addValue('label', 'Two')
51 ->addValue('value', 2)
52 ->addValue('is_default', NULL)
55 $this->assertTrue(OptionValue
::get(FALSE)->addWhere('id', '=', $defaultId)->execute()->first()['is_default']);
58 public function testUpdateWeights() {
59 $getValues = function($groupName) {
60 return OptionValue
::get(FALSE)
61 ->addWhere('option_group_id.name', '=', $groupName)
62 ->addOrderBy('weight')
63 ->execute()->indexBy('value')->column('weight');
66 // Create 2 option groups. Control group is to ensure updating one doesn't affect the other
67 foreach (['controlGroup', 'experimentalGroup'] as $groupName) {
68 OptionGroup
::create(FALSE)
69 ->addValue('name', $groupName)
72 ['label' => 'One', 'value' => 1],
73 ['label' => 'Two', 'value' => 2],
74 ['label' => 'Three', 'value' => 3],
75 ['label' => 'Four', 'value' => 4],
77 OptionValue
::save(FALSE)
78 ->setRecords($sampleData)
79 ->addDefault('option_group_id.name', $groupName)
81 // Default weights should have been set during create
82 $this->assertEquals([1 => 1, 2 => 2, 3 => 3, 4 => 4], $getValues($groupName));
85 // Move first option to last position
86 OptionValue
::update(FALSE)
87 ->addWhere('option_group_id.name', '=', 'experimentalGroup')
88 ->addWhere('value', '=', 1)
89 ->addValue('weight', 4)
91 // Experimental group should be updated, control group should not
92 $this->assertEquals([2 => 1, 3 => 2, 4 => 3, 1 => 4], $getValues('experimentalGroup'));
93 $this->assertEquals([1 => 1, 2 => 2, 3 => 3, 4 => 4], $getValues('controlGroup'));
95 // Move 2nd (new first) option to last position
96 OptionValue
::update(FALSE)
97 ->addWhere('option_group_id.name', '=', 'experimentalGroup')
98 ->addWhere('value', '=', 2)
99 ->addValue('weight', 4)
101 // Experimental group should be updated, control group should not
102 $this->assertEquals([3 => 1, 4 => 2, 1 => 3, 2 => 4], $getValues('experimentalGroup'));
103 $this->assertEquals([1 => 1, 2 => 2, 3 => 3, 4 => 4], $getValues('controlGroup'));
105 // Move last option to first position
106 OptionValue
::update(FALSE)
107 ->addWhere('option_group_id.name', '=', 'experimentalGroup')
108 ->addWhere('value', '=', 2)
109 ->addValue('weight', 1)
111 // Experimental group should be updated, control group should not
112 $this->assertEquals([2 => 1, 3 => 2, 4 => 3, 1 => 4], $getValues('experimentalGroup'));
113 $this->assertEquals([1 => 1, 2 => 2, 3 => 3, 4 => 4], $getValues('controlGroup'));
115 // Same thing again - should have no impact
116 OptionValue
::update(FALSE)
117 ->addWhere('option_group_id.name', '=', 'experimentalGroup')
118 ->addWhere('value', '=', 2)
119 ->addValue('weight', 1)
121 // Nothing should have changed
122 $this->assertEquals([2 => 1, 3 => 2, 4 => 3, 1 => 4], $getValues('experimentalGroup'));
123 $this->assertEquals([1 => 1, 2 => 2, 3 => 3, 4 => 4], $getValues('controlGroup'));