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\Contact
;
24 use Civi\Api4\MembershipType
;
25 use Civi\Test\TransactionalInterface
;
30 class MembershipTest
extends UnitTestCase
implements TransactionalInterface
{
32 public function testUpdateWeights() {
33 $getValues = function($domain) {
34 return MembershipType
::get(FALSE)
35 ->addWhere('domain_id.name', '=', $domain)
36 ->addOrderBy('weight')
37 ->execute()->indexBy('name')->column('weight');
40 // Create 2 domains. Control domain is to ensure updating one doesn't affect the other
41 foreach (['controlDomain', 'experimentalDomain'] as $domain) {
43 ->addValue('name', $domain)
44 ->addValue('version', \CRM_Utils_System
::version())
52 MembershipType
::save(FALSE)
53 ->setRecords($sampleData)
54 ->addDefault('domain_id.name', $domain)
55 ->addDefault('financial_type_id', 1)
56 ->addDefault('duration_unit', 'day')
57 ->addDefault('period_type', 'rolling')
58 ->addDefault('member_of_contact_id', Contact
::create(FALSE)
59 ->addValue('organization_name', $domain)->execute()->first()['id'])
61 $this->assertEquals(['One' => 1, 'Two' => 2, 'Three' => 3, 'Four' => 4], $getValues($domain));
64 // Move first option to third position
65 MembershipType
::update(FALSE)
66 ->addWhere('domain_id.name', '=', 'experimentalDomain')
67 ->addWhere('name', '=', 'One')
68 ->addValue('weight', 3)
70 // Experimental domain should be updated, control domain should not
71 $this->assertEquals(['Two' => 1, 'Three' => 2, 'One' => 3, 'Four' => 4], $getValues('experimentalDomain'));
72 $this->assertEquals(['One' => 1, 'Two' => 2, 'Three' => 3, 'Four' => 4], $getValues('controlDomain'));
77 * Test getting options
79 public function testGetOptions(): void
{
80 $fields = MembershipType
::getFields(FALSE)
81 ->setLoadOptions(['name', 'id', 'label'])
82 ->execute()->indexBy('name');
83 $this->assertEquals('rolling', $fields['period_type']['options'][0]['name']);
84 $this->assertEquals('rolling', $fields['period_type']['options'][0]['id']);
85 $this->assertEquals('Rolling', $fields['period_type']['options'][0]['label']);