| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 28 | |
| 29 | /** |
| 30 | * Class CRM_Extension_Manager_PaymentTest |
| 31 | */ |
| 32 | class CRM_Extension_Manager_PaymentTest extends CiviUnitTestCase { |
| 33 | |
| 34 | public function setUp() { |
| 35 | parent::setUp(); |
| 36 | if (class_exists('test_extension_manager_paymenttest')) { |
| 37 | test_extension_manager_paymenttest::$counts = array(); |
| 38 | } |
| 39 | $this->system = new CRM_Extension_System(array( |
| 40 | 'extensionsDir' => '', |
| 41 | 'extensionsURL' => '', |
| 42 | )); |
| 43 | $this->quickCleanup(array('civicrm_payment_processor')); |
| 44 | } |
| 45 | |
| 46 | public function tearDown() { |
| 47 | parent::tearDown(); |
| 48 | $this->system = NULL; |
| 49 | $this->quickCleanup(array('civicrm_payment_processor')); |
| 50 | CRM_Core_DAO::executeQuery('DELETE FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Install an extension with a valid type name. |
| 55 | */ |
| 56 | public function testInstallDisableUninstall() { |
| 57 | $manager = $this->system->getManager(); |
| 58 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 59 | $manager->install(array('test.extension.manager.paymenttest')); |
| 60 | $this->assertEquals(1, test_extension_manager_paymenttest::$counts['install']); |
| 61 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest" AND is_active = 1'); |
| 62 | |
| 63 | $manager->disable(array('test.extension.manager.paymenttest')); |
| 64 | $this->assertEquals(1, test_extension_manager_paymenttest::$counts['disable']); |
| 65 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 66 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest" AND is_active = 0'); |
| 67 | |
| 68 | $manager->uninstall(array('test.extension.manager.paymenttest')); |
| 69 | $this->assertEquals(1, test_extension_manager_paymenttest::$counts['uninstall']); |
| 70 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Install an extension with a valid type name. |
| 75 | */ |
| 76 | public function testInstallDisableEnable() { |
| 77 | $manager = $this->system->getManager(); |
| 78 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 79 | |
| 80 | $manager->install(array('test.extension.manager.paymenttest')); |
| 81 | $this->assertEquals(1, test_extension_manager_paymenttest::$counts['install']); |
| 82 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest" AND is_active = 1'); |
| 83 | |
| 84 | $manager->disable(array('test.extension.manager.paymenttest')); |
| 85 | $this->assertEquals(1, test_extension_manager_paymenttest::$counts['disable']); |
| 86 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 87 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest" AND is_active = 0'); |
| 88 | |
| 89 | $manager->enable(array('test.extension.manager.paymenttest')); |
| 90 | $this->assertEquals(1, test_extension_manager_paymenttest::$counts['enable']); |
| 91 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 92 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest" AND is_active = 1'); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Install an extension and create a payment processor which uses it. |
| 97 | * Attempts to uninstall fail |
| 98 | */ |
| 99 | public function testInstall_Add_FailUninstall() { |
| 100 | $manager = $this->system->getManager(); |
| 101 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 102 | |
| 103 | $manager->install(array('test.extension.manager.paymenttest')); |
| 104 | $this->assertEquals(1, test_extension_manager_paymenttest::$counts['install']); |
| 105 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest" AND is_active = 1'); |
| 106 | $payment_processor_type_id = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 107 | |
| 108 | $ppDAO = CRM_Financial_BAO_PaymentProcessor::create(array( |
| 109 | 'payment_processor_type_id' => $payment_processor_type_id, |
| 110 | 'domain_id' => CRM_Core_Config::domainID(), |
| 111 | )); |
| 112 | |
| 113 | $manager->disable(array('test.extension.manager.paymenttest')); |
| 114 | $this->assertEquals(1, test_extension_manager_paymenttest::$counts['disable']); |
| 115 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 116 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest" AND is_active = 0'); |
| 117 | |
| 118 | // first attempt to uninstall -- fail |
| 119 | try { |
| 120 | $manager->uninstall(array('test.extension.manager.paymenttest')); |
| 121 | $this->fail('Failed to catch expected exception'); |
| 122 | } |
| 123 | catch (CRM_Extension_Exception_DependencyException $e) { |
| 124 | } |
| 125 | $this->assertEquals(0, test_extension_manager_paymenttest::getCount('uninstall')); |
| 126 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 127 | |
| 128 | $ppDAO->delete(); |
| 129 | |
| 130 | // second attempt to uninstall -- ok |
| 131 | $manager->uninstall(array('test.extension.manager.paymenttest')); |
| 132 | $this->assertEquals(1, test_extension_manager_paymenttest::getCount('uninstall')); |
| 133 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_payment_processor_type WHERE class_name = "test.extension.manager.paymenttest"'); |
| 134 | } |
| 135 | |
| 136 | } |