Skip to content

Commit

Permalink
dev/core#2846 Tests for start/end date form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
JKingsnorth committed Sep 20, 2021
1 parent 9594223 commit 4347c49
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 30 deletions.
5 changes: 1 addition & 4 deletions CRM/Campaign/Form/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,10 @@ public function buildQuickForm() {
* 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
Expand Down
76 changes: 50 additions & 26 deletions tests/phpunit/CRM/Campaign/Form/CampaignTest.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* Test APIv3 civicrm_contribute_* functions
*
* @package CiviCRM_APIv3
* @subpackage API_Contribution
* @group headless
*/
class CRM_Campaign_Form_CampaignTest extends CiviUnitTestCase {

/**
* Test the submit function on the contribution page.
* Set up a correct array of form values.
*
* @return array
*/
private function getCorrectFormFields($thousandSeparator) {
return [
'goal_revenue' => '$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
*
Expand All @@ -30,17 +31,40 @@ public function testSubmit($thousandSeparator) {
$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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

class CRM_Contribute_Form_ContributionPage_SettingsTest extends CiviUnitTestCase {

/**
* Set up a correct array of form values.
*
* @return array
*/
private function getCorrectFormFields() {
return [
'title' => '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);
}

}
49 changes: 49 additions & 0 deletions tests/phpunit/CRM/Event/Form/ManageEvent/EventInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

class CRM_Event_Form_ManageEvent_EventInfoTest extends CiviUnitTestCase {

/**
* Set up a correct array of form values.
*
* @return array
*/
private function getCorrectFormFields() {
return [
'title' => '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);
}

}
44 changes: 44 additions & 0 deletions tests/phpunit/CRM/Event/Form/ManageEvent/RegistrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

class CRM_Event_Form_ManageEvent_RegistrationTest extends CiviUnitTestCase {

/**
* Set up a correct array of form values.
* @todo More fields are required for formRule to return no errors
*
* @return array
*/
private function getCorrectFormFields() {
return [
'is_online_registration' => 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);
}

}
28 changes: 28 additions & 0 deletions tests/phpunit/CRM/Price/Form/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ private function initializeFieldParameters($params) {
'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++) {
Expand Down Expand Up @@ -300,4 +302,30 @@ public function testPriceFieldFormRuleOnMembership() {
], $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);
}

}

0 comments on commit 4347c49

Please sign in to comment.