| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group headless |
| 5 | */ |
| 6 | class CRM_Core_FormTest extends CiviUnitTestCase { |
| 7 | |
| 8 | /** |
| 9 | * Simulate opening various forms. All we're looking to do here is |
| 10 | * see if any warnings or notices come up, the equivalent of red boxes |
| 11 | * on the screen, but which are hidden when using popup forms. |
| 12 | * So no assertions required. |
| 13 | * |
| 14 | * @param string $classname |
| 15 | * @param callable $additionalSetup |
| 16 | * Function that performs some additional setup steps specific to the form |
| 17 | * |
| 18 | * @dataProvider formClassList |
| 19 | */ |
| 20 | public function testOpeningForms(string $classname, callable $additionalSetup) { |
| 21 | $form = $this->getFormObject($classname); |
| 22 | |
| 23 | // call the callable parameter we were passed in |
| 24 | $additionalSetup($form); |
| 25 | |
| 26 | // typical quickform/smarty flow |
| 27 | $form->preProcess(); |
| 28 | $form->buildQuickForm(); |
| 29 | $form->setDefaultValues(); |
| 30 | $form->assign('action', $form->_action ?? CRM_Core_Action::UPDATE); |
| 31 | $form->getTemplate()->fetch($form->getTemplateFileName()); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Dataprovider for testOpeningForms(). |
| 36 | * TODO: Add more forms! |
| 37 | * |
| 38 | * @return array |
| 39 | * See first one below for description. |
| 40 | */ |
| 41 | public function formClassList() { |
| 42 | return [ |
| 43 | // Array key is descriptive term to make it clearer which form it is when it fails. |
| 44 | 'Add New Tag' => [ |
| 45 | // classname |
| 46 | 'CRM_Tag_Form_Edit', |
| 47 | // Function that performs some class-specific additional setup steps. |
| 48 | // If there's a lot of complex steps then that suggests it should have |
| 49 | // its own test elsewhere and doesn't fit well here. |
| 50 | function(CRM_Core_Form $form) {}, |
| 51 | ], |
| 52 | 'Assign Account to Financial Type' => [ |
| 53 | 'CRM_Financial_Form_FinancialTypeAccount', |
| 54 | function(CRM_Core_Form $form) { |
| 55 | $form->set('id', 1); |
| 56 | $form->set('aid', 1); |
| 57 | $form->_action = CRM_Core_Action::ADD; |
| 58 | }, |
| 59 | ], |
| 60 | // This one is a bit flawed but the only point of this test is to catch |
| 61 | // simple stuff. This will catch e.g. "undefined index" and similar. |
| 62 | 'Find Contacts' => [ |
| 63 | 'CRM_Contact_Form_Search_Basic', |
| 64 | function(CRM_Core_Form $form) { |
| 65 | $form->_action = CRM_Core_Action::BASIC; |
| 66 | }, |
| 67 | ], |
| 68 | ]; |
| 69 | } |
| 70 | |
| 71 | } |