Merge pull request #23309 from civicrm/5.49
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / MembershipTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19 namespace api\v4\Entity;
20
21 use api\v4\UnitTestCase;
22 use Civi\Api4\Contact;
23 use Civi\Api4\Domain;
24 use Civi\Api4\MembershipType;
25 use Civi\Test\TransactionalInterface;
26
27 /**
28 * @group headless
29 */
30 class MembershipTest extends UnitTestCase implements TransactionalInterface {
31
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');
38 };
39
40 // Create 2 domains. Control domain is to ensure updating one doesn't affect the other
41 foreach (['controlDomain', 'experimentalDomain'] as $domain) {
42 Domain::create(FALSE)
43 ->addValue('name', $domain)
44 ->addValue('version', \CRM_Utils_System::version())
45 ->execute();
46 $sampleData = [
47 ['name' => 'One'],
48 ['name' => 'Two'],
49 ['name' => 'Three'],
50 ['name' => 'Four'],
51 ];
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'])
60 ->execute();
61 $this->assertEquals(['One' => 1, 'Two' => 2, 'Three' => 3, 'Four' => 4], $getValues($domain));
62 }
63
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)
69 ->execute();
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'));
73
74 }
75
76 /**
77 * Test getting options
78 */
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']);
86 }
87
88 }