| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Test APIv3 civicrm_PCP_* functions |
| 14 | * |
| 15 | * @package CiviCRM_APIv3 |
| 16 | * @subpackage API_Campaign |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * All API should contain at minimum a success test for each |
| 21 | * function - in this case - create, get & delete |
| 22 | * In addition any extra functionality should be tested & documented |
| 23 | * |
| 24 | * Failure tests should be added for specific api behaviours but note that |
| 25 | * many generic patterns are tested in the syntax conformance test |
| 26 | * |
| 27 | * @author eileen |
| 28 | * |
| 29 | * @group headless |
| 30 | */ |
| 31 | class api_v3_PcpTest extends CiviUnitTestCase { |
| 32 | protected $params; |
| 33 | protected $entity = 'Pcp'; |
| 34 | public $DBResetRequired = TRUE; |
| 35 | |
| 36 | public function setUp() { |
| 37 | $this->params = [ |
| 38 | 'title' => "Pcp title", |
| 39 | 'contact_id' => 1, |
| 40 | 'page_id' => 1, |
| 41 | 'pcp_block_id' => 1, |
| 42 | ]; |
| 43 | parent::setUp(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Test create function succeeds. |
| 48 | */ |
| 49 | public function testCreatePcp() { |
| 50 | $result = $this->callAPIAndDocument('Pcp', 'create', $this->params, |
| 51 | __FUNCTION__, __FILE__); |
| 52 | $this->getAndCheck($this->params, $result['id'], $this->entity); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Test disable a PCP succeeds. |
| 57 | */ |
| 58 | public function testDisablePcp() { |
| 59 | $result = civicrm_api3('Pcp', 'create', $this->params); |
| 60 | civicrm_api3('Pcp', 'create', ['id' => $result['id'], 'is_active' => 0]); |
| 61 | $this->getAndCheck($this->params + ['is_active' => 0], $result['id'], $this->entity); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Test get function succeeds. |
| 66 | * |
| 67 | * This is actually largely tested in the get |
| 68 | * action on create. Add extra checks for any 'special' return values or |
| 69 | * behaviours |
| 70 | */ |
| 71 | public function testGetPcp() { |
| 72 | $this->createTestEntity(); |
| 73 | $result = $this->callAPIAndDocument('Pcp', 'get', $this->params, |
| 74 | __FUNCTION__, __FILE__); |
| 75 | $this->assertEquals(1, $result['count']); |
| 76 | $this->assertNotNull($result['values'][$result['id']]['id']); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Check the delete function succeeds. |
| 81 | */ |
| 82 | public function testDeletePcp() { |
| 83 | $entity = $this->createTestEntity(); |
| 84 | $checkCreated = $this->callAPISuccess($this->entity, 'get', |
| 85 | ['id' => $entity['id']]); |
| 86 | $this->assertEquals(1, $checkCreated['count']); |
| 87 | $this->callAPIAndDocument('Pcp', 'delete', |
| 88 | ['id' => $entity['id']], __FUNCTION__, __FILE__); |
| 89 | $checkDeleted = $this->callAPISuccess($this->entity, 'get', |
| 90 | ['id' => $entity['id']]); |
| 91 | $this->assertEquals(0, $checkDeleted['count']); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Test & document chained delete pattern. |
| 96 | * |
| 97 | * Note that explanation of the pattern |
| 98 | * is best put in the $description variable as it will then be displayed in the |
| 99 | * test generated examples. (these are to be found in the api/examples folder). |
| 100 | */ |
| 101 | public function testGetPcpChainDelete() { |
| 102 | $description = "Demonstrates get + delete in the same call."; |
| 103 | $subfile = 'ChainedGetDelete'; |
| 104 | $params = ['title' => "Pcp title", 'api.Pcp.delete' => 1]; |
| 105 | $this->callAPISuccess('Pcp', 'create', $this->params); |
| 106 | $this->callAPIAndDocument('Pcp', 'get', $params, __FUNCTION__, |
| 107 | __FILE__, $description, $subfile); |
| 108 | $this->assertEquals(0, $this->callAPISuccess('Pcp', 'getcount', [])); |
| 109 | } |
| 110 | |
| 111 | } |