Merge branch 'master' into master-civimail-abtest
[civicrm-core.git] / tests / phpunit / api / v3 / SurveyTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30
31 /**
32 * Test APIv3 civicrm_survey_* functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Campaign
36 */
37
38 require_once 'CiviTest/CiviUnitTestCase.php';
39
40 /**
41 * All API should contain at minimum a success test for each
42 * function - in this case - create, get & delete
43 * In addition any extra functionality should be tested & documented
44 *
45 * Failure tests should be added for specific api behaviours but note that
46 * many generic patterns are tested in the syntax conformance test
47 *
48 * @author eileen
49 *
50 */
51 class api_v3_SurveyTest extends CiviUnitTestCase {
52 protected $params;
53 protected $entity = 'survey';
54 public $DBResetRequired = FALSE;
55
56
57 function setUp() {
58 $phoneBankActivityTypeID = $this->callAPISuccessGetValue('Option_value', array('label' => 'PhoneBank', 'return' => 'value'), 'integer');
59 $this->enableCiviCampaign();
60 $this->params = array(
61 'title' => "survey title",
62 'activity_type_id' => $phoneBankActivityTypeID,
63 'max_number_of_contacts' => 12,
64 'instructions' => "Call people, ask for money",
65 );
66 parent::setUp();
67 }
68
69 /**
70 * Here we clean up any test data we created.
71 * Note that the quickCleanup function turns off Foreign keys first
72 * so will not remove related entities
73 */
74 function tearDown() {
75 $tablesToTruncate = array('civicrm_survey');
76 $this->quickCleanup($tablesToTruncate);
77 }
78
79 /**
80 * Test create function succeeds
81 */
82 public function testCreateSurvey() {
83 $result = $this->callAPIAndDocument('survey', 'create', $this->params, __FUNCTION__, __FILE__);
84 $this->getAndCheck($this->params, $result['id'], $this->entity);
85 }
86
87 /**
88 * Test get function succeeds (this is actually largely tested in the get
89 * action on create. Add extra checks for any 'special' return values or
90 * behaviours
91 *
92 */
93 public function testGetSurvey() {
94 $this->createTestEntity();
95 $result = $this->callAPIAndDocument('survey', 'get', $this->params, __FUNCTION__, __FILE__);
96 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
97 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__);
98 }
99
100 /**
101 * Check the delete function succeeds
102 */
103 public function testDeleteSurvey() {
104 $entity = $this->createTestEntity();
105 $result = $this->callAPIAndDocument('survey', 'delete', array('id' => $entity['id']), __FUNCTION__, __FILE__);
106 $checkDeleted = $this->callAPISuccess($this->entity, 'get', array(
107 ));
108 $this->assertEquals(0, $checkDeleted['count'], 'In line ' . __LINE__);
109 }
110
111 /**
112 * Test & document chained delete pattern. Note that explanation of the pattern
113 * is best put in the $description variable as it will then be displayed in the
114 * test generated examples. (these are to be found in the api/examples folder)
115 *
116 */
117 public function testGetSurveyChainDelete() {
118 $description = "demonstrates get + delete in the same call";
119 $subfile = 'ChainedGetDelete';
120 $params = array(
121 'title' => "survey title",
122 'api.survey.delete' => 1,
123 );
124 $result = $this->callAPISuccess('survey', 'create', $this->params);
125 $result = $this->callAPIAndDocument('survey', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
126 $this->assertEquals(0, $this->callAPISuccess('survey', 'getcount', array()), 'In line ' . __LINE__);
127 }
128
129 }
130