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 a726c90
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 26 deletions.
71 changes: 45 additions & 26 deletions tests/phpunit/CRM/Campaign/Form/CampaignTest.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
<?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 {

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 contribution page.
* Test the submit function on the campaign page.
*
* @param string $thousandSeparator
*
Expand All @@ -30,17 +26,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,47 @@
<?php

class CRM_Contribute_Form_ContributionPage_SettingsTest extends CiviUnitTestCase {

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['registration_end_date'] = '00:01';
$form = new CRM_Contribute_Form_ContributionPage_Settings();
$validationResult = \CRM_Contribute_Form_ContributionPage_Settings::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_Contribute_Form_ContributionPage_Settings();
$validationResult = \CRM_Contribute_Form_ContributionPage_Settings::formRule($values, [], $form);
$this->assertArrayHasKey('registration_end_date', $validationResult);
}

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

class CRM_Event_Form_ManageEvent_EventInfoTest extends CiviUnitTestCase {

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);
}

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

class CRM_Event_Form_ManageEvent_RegistrationTest extends CiviUnitTestCase {

// @todo More fields are required for formRule to return no errors
private function getCorrectFormFields() {
return [
'is_online_registration' => 1,
'registration_start_date' => date('Y-m-d'),
'registration_end_date' => date('Y-m-d', time() + 86400),
];
}

/**
* 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);
}

}
24 changes: 24 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,26 @@ public function testPriceFieldFormRuleOnMembership() {
], $errors);
}

/**
* Test end date not allowed with only 'time' part.
*/
public function testEndDateWithoutDateNotAllowed() {
$values = $this->initializeFieldParameters([
'expire_on' >= '00:01',
]);
$validationResult = \CRM_Price_Form_Field::formRule($values);
$this->assertArrayHasKey('expire_on', $validationResult);
}

/**
* Test end date must be after start date.
*/
public function testEndDateBeforeStartDateNotAllowed() {
$values = $this->initializeFieldParameters([
'expire_on' >= '1900-01-01 00:00',
]);
$validationResult = \CRM_Price_Form_Field::formRule($values);
$this->assertArrayHasKey('expire_on', $validationResult);
}

}

0 comments on commit a726c90

Please sign in to comment.