CRM-13014 support userLoginFinalize() for UF classes (fix civimobile)
[civicrm-core.git] / tests / phpunit / api / v3 / OptionGroupTest.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4
5require_once 'CiviTest/CiviUnitTestCase.php';
6class api_v3_OptionGroupTest extends CiviUnitTestCase {
7 protected $_apiversion;
8 public $_eNoticeCompliant = TRUE;
9
10 function setUp() {
11 $this->_apiversion = 3;
12 parent::setUp();
13 }
14
15 function tearDown() {}
16 /*
17 * Good to test option group as a representative on the Camel Case
18 */
19
20 public function testGetOptionGroupGetFields() {
21 $result = civicrm_api('option_group', 'getfields', array('version' => 3));
22 $this->assertFalse(empty($result['values']), 'In line ' . __LINE__);
23 }
24 public function testGetOptionGroupGetFieldsCreateAction() {
25 $result = civicrm_api('option_group', 'getfields', array('action' => 'create', 'version' => 3));
26 $this->assertFalse(empty($result['values']), 'In line ' . __LINE__);
27 $this->assertEquals($result['values']['name']['api.unique'], 1);
28 }
29
30 public function testGetOptionGroupByID() {
31 $result = civicrm_api('option_group', 'get', array('id' => 1, 'version' => 3));
32 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
33 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
34 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
35 }
36
37 public function testGetOptionGroupByName() {
38 $params = array('name' => 'preferred_communication_method', 'version' => 3);
39 $result = civicrm_api('option_group', 'get', $params);
40 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
41 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
42 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
43 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
44 }
45
46 public function testGetOptionGroup() {
47 $result = civicrm_api('option_group', 'get', array('version' => 3));
48 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
49 $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__);
50 }
51
52 public function testGetOptionDoesNotExist() {
53 $result = civicrm_api('option_group', 'get', array('name' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB', 'version' => 3));
54 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
55 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__);
56 }
57 public function testGetOptionCreateSuccess() {
58 $params = array('version' => $this->_apiversion, 'sequential' => 1, 'name' => 'civicrm_event.amount.560', 'is_reserved' => 1, 'is_active' => 1, 'api.OptionValue.create' => array('label' => 'workshop', 'value' => 35, 'is_default' => 1, 'is_active' => 1, 'format.only_id' => 1));
59 $result = civicrm_api('OptionGroup', 'create', $params);
60 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
61 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
62 $this->assertEquals('civicrm_event.amount.560', $result['values'][0]['name'], 'In line ' . __LINE__);
63 $this->assertTrue(is_integer($result['values'][0]['api.OptionValue.create']));
64 $this->assertGreaterThan(0, $result['values'][0]['api.OptionValue.create']);
65 civicrm_api('OptionGroup', 'delete', array('version' => 3, 'id' => $result['id']));
66 }
67 /*
68 * Test the error message when a failure is due to a key duplication issue
69 */
70
71 public function testGetOptionCreateFailOnDuplicate() {
72 $params = array(
73 'version' => $this->_apiversion,
74 'sequential' => 1,
75 'name' => 'civicrm_dup entry',
76 'is_reserved' => 1,
77 'is_active' => 1,
78 );
79 $result1 = civicrm_api('OptionGroup', 'create', $params);
80 $this->assertAPISuccess($result1);
81 $result = civicrm_api('OptionGroup', 'create', $params);
82 civicrm_api('OptionGroup', 'delete', array('version' => 3, 'id' => $result1['id']));
83 $this->assertEquals("Field: `name` must be unique. An conflicting entity already exists - id: " . $result1['id'], $result['error_message']);
84 }
85
86 /*
87 * Test that transaction is completely rolled back on fail.
88 * Also check error returned
89 */
90
91 public function testGetOptionCreateFailRollback() {
92 $countFirst = civicrm_api('OptionGroup', 'getcount', array(
93 'version' => 3,
94 'options' => array('limit' => 5000),
95 )
96 );
97 $params = array(
98 'version' => $this->_apiversion,
99 'sequential' => 1,
100 'name' => 'civicrm_rolback_test',
101 'is_reserved' => 1,
102 'is_active' => 1,
103 'api.OptionValue.create' => array(
104 'label' => 'invalid entry',
105 'value' => 35,
106 'domain_id' => 999,
107 'is_active' => '0',
108 'debug' => 0,
109 ),
110 );
111 $result = civicrm_api('OptionGroup', 'create', $params);
112 $this->assertEquals(1, $result['is_error'], 'Error should be passed up to top level In line ' . __LINE__);
113 $countAfter = civicrm_api('OptionGroup', 'getcount', array(
114 'version' => 3,
115 'options' => array('limit' => 5000),
116 )
117 );
118 $this->assertEquals($countFirst, $countAfter,
119 'Count of option groups should not have changed due to rollback triggered by option value In line ' . __LINE__
120 );
121 }
122}
123