dev/core#2846 Tests for start/end date form validation
authorJohn Kingsnorth <john@johnkingsnorth.co.uk>
Mon, 20 Sep 2021 20:49:20 +0000 (21:49 +0100)
committerJohn Kingsnorth <john@johnkingsnorth.co.uk>
Mon, 20 Sep 2021 21:29:33 +0000 (22:29 +0100)
CRM/Campaign/Form/Campaign.php
tests/phpunit/CRM/Campaign/Form/CampaignTest.php
tests/phpunit/CRM/Contribute/Form/ContributionPage/SettingsTest.php [new file with mode: 0644]
tests/phpunit/CRM/Event/Form/ManageEvent/EventInfoTest.php [new file with mode: 0644]
tests/phpunit/CRM/Event/Form/ManageEvent/RegistrationTest.php [new file with mode: 0644]
tests/phpunit/CRM/Price/Form/FieldTest.php

index d3923324a6f05c3ec1cdbae89afc9d4e1d308271..838405521b4f2e45f88c2a07d79506442805706a 100644 (file)
@@ -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
index 137feedac74af87ba2188c345cc25d97a7461507..1f845081b5aabfd9ae23a547aad8722d3c16e5c2 100644 (file)
@@ -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
    *
@@ -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 (file)
index 0000000..9e01fba
--- /dev/null
@@ -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);
+  }
+
+}
diff --git a/tests/phpunit/CRM/Event/Form/ManageEvent/EventInfoTest.php b/tests/phpunit/CRM/Event/Form/ManageEvent/EventInfoTest.php
new file mode 100644 (file)
index 0000000..6a9d7ce
--- /dev/null
@@ -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);
+  }
+
+}
diff --git a/tests/phpunit/CRM/Event/Form/ManageEvent/RegistrationTest.php b/tests/phpunit/CRM/Event/Form/ManageEvent/RegistrationTest.php
new file mode 100644 (file)
index 0000000..8eb86d9
--- /dev/null
@@ -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);
+  }
+
+}
index 0d58a58ee13aebdd9660b6ef9bf9aad69d95263a..6370d5b9aa21254b1676009b70d863171e5e7143 100644 (file)
@@ -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);
+  }
+
 }