From: John Kingsnorth Date: Mon, 20 Sep 2021 20:49:20 +0000 (+0100) Subject: dev/core#2846 Tests for start/end date form validation X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=4347c49a4e341b21652039f14b8e28063652377c;p=civicrm-core.git dev/core#2846 Tests for start/end date form validation --- diff --git a/CRM/Campaign/Form/Campaign.php b/CRM/Campaign/Form/Campaign.php index d3923324a6..838405521b 100644 --- a/CRM/Campaign/Form/Campaign.php +++ b/CRM/Campaign/Form/Campaign.php @@ -258,13 +258,10 @@ class CRM_Campaign_Form_Campaign extends CRM_Core_Form { * All local rules are added near the element * * @param $fields - * @param $files - * @param $errors * * @return bool|array - * @see valid_date */ - public static function formRule($fields, $files, $errors) { + public static function formRule($fields) { $errors = []; // Validate start/end date inputs diff --git a/tests/phpunit/CRM/Campaign/Form/CampaignTest.php b/tests/phpunit/CRM/Campaign/Form/CampaignTest.php index 137feedac7..1f845081b5 100644 --- a/tests/phpunit/CRM/Campaign/Form/CampaignTest.php +++ b/tests/phpunit/CRM/Campaign/Form/CampaignTest.php @@ -1,25 +1,26 @@ '$10' . $thousandSeparator . '000', + 'is_active' => 1, + 'title' => 'Test Campaign', + 'start_date' => date('Y-m-d'), + 'includeGroups' => [], + 'custom' => [], + 'campaign_type_id' => 1, + ]; + } + + /** + * Test the submit function on the campaign page. * * @param string $thousandSeparator * @@ -30,17 +31,40 @@ class CRM_Campaign_Form_CampaignTest extends CiviUnitTestCase { $this->createLoggedInUser(); $form = new CRM_Campaign_Form_Campaign(); $form->_action = CRM_Core_Action::ADD; - $result = CRM_Campaign_Form_Campaign::Submit([ - 'goal_revenue' => '$10' . $thousandSeparator . '000', - 'is_active' => 1, - 'title' => 'Test Campaign', - 'start_date' => date('Y-m-d'), - 'includeGroups' => [], - 'custom' => [], - 'campaign_type_id' => 1, - ], $form); + $values = $this->getCorrectFormFields($thousandSeparator); + $result = CRM_Campaign_Form_Campaign::Submit($values, $form); $campaign = $this->callAPISuccess('campaign', 'get', ['id' => $result['id']]); $this->assertEquals('10000.00', $campaign['values'][$campaign['id']]['goal_revenue']); } + /** + * Test end date not allowed with only 'time' part. + * + * @param string $thousandSeparator + * + * @dataProvider getThousandSeparators + */ + public function testEndDateWithoutDateNotAllowed($thousandSeparator) { + $this->setCurrencySeparators($thousandSeparator); + $values = $this->getCorrectFormFields($thousandSeparator); + $values['end_date'] = '00:01'; + $validationResult = \CRM_Campaign_Form_Campaign::formRule($values); + $this->assertArrayHasKey('end_date', $validationResult); + } + + /** + * Test end date must be after start date. + * + * @param string $thousandSeparator + * + * @dataProvider getThousandSeparators + */ + public function testEndDateBeforeStartDateNotAllowed($thousandSeparator) { + $this->setCurrencySeparators($thousandSeparator); + $values = $this->getCorrectFormFields($thousandSeparator); + $values['end_date'] = '1900-01-01 00:00'; + $validationResult = \CRM_Campaign_Form_Campaign::formRule($values); + $this->assertArrayHasKey('end_date', $validationResult); + } + } diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionPage/SettingsTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionPage/SettingsTest.php new file mode 100644 index 0000000000..9e01fba3a4 --- /dev/null +++ b/tests/phpunit/CRM/Contribute/Form/ContributionPage/SettingsTest.php @@ -0,0 +1,51 @@ + 'Test contribution page', + 'financial_type_id' => 1, + 'start_date' => date('Y-m-d'), + 'end_date' => date('Y-m-d', time() + 86400), + ]; + } + + /** + * Test correct form submission. + */ + public function testValidFormSubmission() { + $values = $this->getCorrectFormFields(); + $form = new CRM_Contribute_Form_ContributionPage_Settings(); + $validationResult = \CRM_Contribute_Form_ContributionPage_Settings::formRule($values, [], $form); + $this->assertEmpty($validationResult); + } + + /** + * Test end date not allowed with only 'time' part. + */ + public function testEndDateWithoutDateNotAllowed() { + $values = $this->getCorrectFormFields(); + $values['end_date'] = '00:01'; + $form = new CRM_Contribute_Form_ContributionPage_Settings(); + $validationResult = \CRM_Contribute_Form_ContributionPage_Settings::formRule($values, [], $form); + $this->assertArrayHasKey('end_date', $validationResult); + } + + /** + * Test end date must be after start date. + */ + public function testEndDateBeforeStartDateNotAllowed() { + $values = $this->getCorrectFormFields(); + $values['end_date'] = '1900-01-01 00:00'; + $form = new CRM_Contribute_Form_ContributionPage_Settings(); + $validationResult = \CRM_Contribute_Form_ContributionPage_Settings::formRule($values, [], $form); + $this->assertArrayHasKey('end_date', $validationResult); + } + +} diff --git a/tests/phpunit/CRM/Event/Form/ManageEvent/EventInfoTest.php b/tests/phpunit/CRM/Event/Form/ManageEvent/EventInfoTest.php new file mode 100644 index 0000000000..6a9d7ce8ec --- /dev/null +++ b/tests/phpunit/CRM/Event/Form/ManageEvent/EventInfoTest.php @@ -0,0 +1,49 @@ + 'A test event', + 'event_type_id' => 1, + 'default_role_id' => 1, + 'start_date' => date('Y-m-d'), + 'end_date' => date('Y-m-d', time() + 86400), + ]; + } + + /** + * Test correct form submission. + */ + public function testValidFormSubmission() { + $values = $this->getCorrectFormFields(); + $validationResult = \CRM_Event_Form_ManageEvent_EventInfo::formRule($values); + $this->assertEmpty($validationResult); + } + + /** + * Test end date not allowed with only 'time' part. + */ + public function testEndDateWithoutDateNotAllowed() { + $values = $this->getCorrectFormFields(); + $values['end_date'] = '00:01'; + $validationResult = \CRM_Event_Form_ManageEvent_EventInfo::formRule($values); + $this->assertArrayHasKey('end_date', $validationResult); + } + + /** + * Test end date must be after start date. + */ + public function testEndDateBeforeStartDateNotAllowed() { + $values = $this->getCorrectFormFields(); + $values['end_date'] = '1900-01-01 00:00'; + $validationResult = \CRM_Event_Form_ManageEvent_EventInfo::formRule($values); + $this->assertArrayHasKey('end_date', $validationResult); + } + +} diff --git a/tests/phpunit/CRM/Event/Form/ManageEvent/RegistrationTest.php b/tests/phpunit/CRM/Event/Form/ManageEvent/RegistrationTest.php new file mode 100644 index 0000000000..8eb86d9ec3 --- /dev/null +++ b/tests/phpunit/CRM/Event/Form/ManageEvent/RegistrationTest.php @@ -0,0 +1,44 @@ + 1, + 'registration_start_date' => date('Y-m-d'), + 'registration_end_date' => date('Y-m-d', time() + 86400), + 'is_email_confirm' => 0, + 'confirm_title' => 'Confirm your registration', + 'thankyou_title' => 'Thank you for your registration', + ]; + } + + /** + * Test end date not allowed with only 'time' part. + */ + public function testEndDateWithoutDateNotAllowed() { + $values = $this->getCorrectFormFields(); + $values['registration_end_date'] = '00:01'; + $form = new CRM_Event_Form_ManageEvent_Registration(); + $validationResult = \CRM_Event_Form_ManageEvent_Registration::formRule($values, [], $form); + $this->assertArrayHasKey('registration_end_date', $validationResult); + } + + /** + * Test end date must be after start date. + */ + public function testEndDateBeforeStartDateNotAllowed() { + $values = $this->getCorrectFormFields(); + $values['registration_end_date'] = '1900-01-01 00:00'; + $form = new CRM_Event_Form_ManageEvent_Registration(); + $validationResult = \CRM_Event_Form_ManageEvent_Registration::formRule($values, [], $form); + $this->assertArrayHasKey('registration_end_date', $validationResult); + } + +} diff --git a/tests/phpunit/CRM/Price/Form/FieldTest.php b/tests/phpunit/CRM/Price/Form/FieldTest.php index 0d58a58ee1..6370d5b9aa 100644 --- a/tests/phpunit/CRM/Price/Form/FieldTest.php +++ b/tests/phpunit/CRM/Price/Form/FieldTest.php @@ -98,6 +98,8 @@ class CRM_Price_Form_FieldTest extends CiviUnitTestCase { 'financial_type_id' => $this->getFinancialTypeId('Event Fee'), 'visibility_id' => $this->visibilityOptionsKeys['public'], 'price' => 10, + 'active_on' => date('Y-m-d'), + 'expire_on' => '', ]; for ($index = 1; $index <= CRM_Price_Form_Field::NUM_OPTION; $index++) { @@ -300,4 +302,30 @@ class CRM_Price_Form_FieldTest extends CiviUnitTestCase { ], $errors); } + /** + * Test end date not allowed with only 'time' part. + */ + public function testEndDateWithoutDateNotAllowed() { + $form = new CRM_Price_Form_Field(); + $form->_action = CRM_Core_Action::ADD; + $values = $this->initializeFieldParameters([ + 'expire_on' => '00:01', + ]); + $validationResult = \CRM_Price_Form_Field::formRule($values, [], $form); + $this->assertArrayHasKey('expire_on', $validationResult); + } + + /** + * Test end date must be after start date. + */ + public function testEndDateBeforeStartDateNotAllowed() { + $form = new CRM_Price_Form_Field(); + $form->_action = CRM_Core_Action::ADD; + $values = $this->initializeFieldParameters([ + 'expire_on' => '1900-01-01 00:00', + ]); + $validationResult = \CRM_Price_Form_Field::formRule($values, [], $form); + $this->assertArrayHasKey('expire_on', $validationResult); + } + }