✔️ add UnitTests

This commit is contained in:
Marc Michalsky forumZFD 2021-04-13 00:20:13 +02:00
parent 7b30a75014
commit 1954f90f79
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
4 changed files with 417 additions and 2 deletions

View file

@ -1,7 +1,7 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" bootstrap="tests/phpunit/bootstrap.php"> <phpunit backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="false" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" bootstrap="tests/phpunit/bootstrap.php">
<testsuites> <testsuites>
<testsuite name="My Test Suite"> <testsuite name="TwingleCampaign Suite">
<directory>./tests/phpunit</directory> <directory>./tests/phpunit</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>

View file

@ -0,0 +1,142 @@
<?php
use CRM_TwingleCampaign_ExtensionUtil as E;
use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;
use CRM_TwingleCampaign_BAO_TwingleProject as TwingleProject;
use \Civi\Test\Api3TestTrait;
/**
* # Collection of tests for the TwingleProject class
*
* - tests the full TwingleProject circle (from Twingle to database to Twingle)
* - tests the input validation
*
* @group headless
*/
class CRM_TwingleCampaign_BAO_TwingleProjectTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
private $project;
private $dummyProject;
private $badDummyProject;
public function setUpHeadless() {
// Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
// See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest
return \Civi\Test::headless()
->installMe(__DIR__)
->apply();
}
/**
* @throws \Exception
*/
public function setUp() {
// Create project from dummy values
$this->dummyProject =
require(E::path() . '/tests/resources/twingle_project_dummy.php');
$this->badDummyProject =
require(E::path() . '/tests/resources/twingle_project_bad_dummy.php');
$this->project = new TwingleProject($this->dummyProject);
$this->project->create(TRUE);
parent::setUp();
}
/**
* @throws \CiviCRM_API3_Exception
*/
public function tearDown() {
// Delete project
$this->project->delete();
unset($this->project);
parent::tearDown();
}
/**
* ## The full TwingleProject circle
* This test simulates a TwingleProject that is fetched from the database and
* than gets instantiated and sent back to Twingle.
*
* It is important that all project values sent to the Twingle API have the
* same data types like when they were retrieved from Twingle. To test this,
* the export array will be compared to the original dummy array.
*
* dummy:array -> project:object -> database -> project:object -> export:array
*
* @throws \CiviCRM_API3_Exception
* @throws \Exception
*/
public function testFullTwingleProjectCircle() {
// Get project via API from database
$project = civicrm_api3(
'TwingleProject',
'getsingle',
['id' => $this->project->getId()]);
$project = new TwingleProject($project, $project['id']);
// Complement project values with dummy values. This is important because
// not all values coming from the Twingle API are stored in the database,
// but the Twingle API requires that all parameters are set.
$project->complement($this->dummyProject);
// Export project
$export = $project->export();
// Check if the project is sent to Twingle in the same state it arrived
$this->assertEquals(
$this->dummyProject,
$export,
'Export values differ from import values.'
);
}
/**
* ## Input validation
* Checks if the input validation works properly.
*
* @throws \CiviCRM_API3_Exception
* @throws \Exception
*/
public function testInputValidation() {
// Get project via API from database
$project = civicrm_api3(
'TwingleProject',
'getsingle',
['id' => $this->project->getId()]);
$project = new TwingleProject($project, $project['id']);
// Check if validation successes with healthy input
$validation_success = $project->validate();
$this->assertTrue(
$validation_success['valid'],
'Validation failed with healthy inputs.'
);
// Update project with values which simulate incorrect user input
$project->update($this->badDummyProject);
// Run validation again
$validation_fail = $project->validate();
// Check if validation failed (as it should)
$this->assertFalse(
$validation_fail['valid'],
'Validation did not fail as expected.'
);
// Check if all 6 wrong inputs were found
$this->assertCount(
6, $validation_fail['messages'],
'Did not find all 6 wrong inputs.'
);
}
}

View file

@ -0,0 +1,136 @@
<?php
return [
"id" => 2243,
"internal_id" => NULL,
"name" => "TestProject",
"organisation_id" => 555,
"slug" => "test_project",
"identifier" => "tw5555555555555",
"type" => "event",
"transaction_type" => NULL,
"project_target" => 1000,
"image_directory" => NULL,
"allow_more" => FALSE,
"last_update" => 1614166801,
"embed" => [
"page" => "https://spenden.twingle.de/my-awesome-ngo/testprojekt/tw555555a555555c/page",
"eventpage" => "https://my-awesome-ngo/event/testprojekt/tw555555a555555c/edit",
"widget" => "<!-- twingle --><!-- twingle -->",
"form" => "<!-- twingle --><!-- twingle -->",
"widget-single" => "<!-- twingle --><!-- twingle -->",
"form-single" => "<!-- twingle --><!-- twingle -->",
"eventall" => "<!-- twingle --><!-- twingle -->",
"eventlist" => "<!-- twingle --><!-- twingle -->",
"eventeditcreate" => "<!-- twingle --><!-- twingle -->",
"giftdonationexample" => "https://spenden.twingle.de/gift/my-awesome-ngo/tw555555a555555c/example",
"letterexample" => "https://spenden.twingle.de/letter/my-awesome-ngo/tw555555a555555c/example",
],
"counter-url" => [
"url" => "https://donationstatus.twingle.de/donation-status/555aaa",
],
"host" => "https://spenden.twingle.de/",
"project_options" => [
"id" => 2237,
"has_confirmation_mail" => FALSE,
"has_confirmation_mail_api" => FALSE,
"has_donation_receipt" => TRUE,
"has_contact_data" => FALSE,
"donation_rhythm" => [
"yearly" => TRUE,
"halfyearly" => FALSE,
"quarterly" => TRUE,
"monthly" => TRUE,
"one_time" => TRUE,
],
"default_rhythm" => "",
"has_newsletter_registration" => FALSE,
"has_postinfo_registration" => FALSE,
"design_background_color" => "not_a_hex", // not a hex
"design_primary_color" => "green", // no hex either
"design_font" => "",
"design_font_color" => "true", // no hex either
"design_button_font_color" => "",
"design_button_font_color_light" => "",
"image" => "",
"bcc_email_address" => "mustermann@awesome", // no valid email
"donation_value_min" => 50, // higher than max
"donation_value_max" => 10,
"donation_value_default" => 30, // higher than max, lower than min
"exclude_contact_fields" => "company,birthday,telephone",
"mandatory_contact_fields" => "firstname,lastname",
"custom_js" => NULL,
"custom_css" => "",
"share_url" => "not_a_url", // no valid url
"has_contact_mandatory" => FALSE,
"has_doi" => TRUE,
"doi_redirect" => NULL,
"has_force_donation_target_buttons" => FALSE,
"has_show_single_target" => FALSE,
"betterpayment_credit_card_theme" => NULL,
"app_js" => NULL,
"slidericon" => "handshake",
"extra_field" => [],
"has_hidden_logo" => FALSE,
"has_projecttarget_as_money" => FALSE,
"rapidmail_recipient_list" => NULL,
"mailchimp_recipient_list" => NULL,
"has_mailchimp_add_all_user" => NULL,
"has_mail_hide_paymentblock" => FALSE,
"has_mail_hide_paymentblock_api" => FALSE,
"cleverreach_group_id" => NULL,
"cleverreach_form_id" => NULL,
"has_donationtarget_textfield" => FALSE,
"has_civi_crm_activated" => FALSE,
"has_step_index" => FALSE,
"gift_donation" => "",
"gift_donation_icon" => NULL,
"gift_donation_image" => NULL,
"languages" => "de",
"has_buttons" => FALSE,
"has_no_slider" => FALSE,
"buttons" => [
"button1" => [
"amount" => "5",
],
"button2" => [
"amount" => "50",
],
"button3" => [
"amount" => "100",
],
"button4" => [
"amount" => "",
],
],
"has_newsletter_namerequest" => FALSE,
"has_show_donator_data" => FALSE,
"has_donation_letter" => FALSE,
"has_donation_letter_api" => FALSE,
"amount_images" => [],
"video_url" => NULL,
],
"payment_methods" => [
"has_paypal" => TRUE,
"has_betterpayment" => FALSE,
"has_sixpay" => FALSE,
"has_banktransfer" => TRUE,
"has_debit" => TRUE,
"has_sofortueberweisung" => FALSE,
"has_betterpayment_creditcard" => FALSE,
"has_mbe4" => FALSE,
"has_generic" => FALSE,
"has_konfipay" => FALSE,
"has_konfipay_recurring" => FALSE,
"has_betterpayment_creditcard_recurring" => FALSE,
"has_paypal_recurring" => FALSE,
"has_debit_recurring" => TRUE,
"has_generic_recurring" => FALSE,
"has_sixpay_recurring" => FALSE,
"has_apple_pay" => FALSE,
"has_apple_pay_recurring" => FALSE,
"has_paydirekt" => FALSE,
"has_paydirekt_recurring" => FALSE,
],
];

View file

@ -0,0 +1,137 @@
<?php
return [
"id" => 2243,
"internal_id" => NULL,
"name" => "TestProject",
"organisation_id" => 555,
"slug" => "test_project",
"identifier" => "tw5555555555555",
"type" => "event",
"transaction_type" => NULL,
"project_target" => 1000,
"image_directory" => NULL,
"allow_more" => FALSE,
"last_update" => 1614166801,
"embed" => [
"page" => "https://spenden.twingle.de/my-awesome-ngo/testprojekt/tw555555a555555c/page",
"eventpage" => "https://my-awesome-ngo/event/testprojekt/tw555555a555555c/edit",
"widget" => "<!-- twingle --><!-- twingle -->",
"form" => "<!-- twingle --><!-- twingle -->",
"widget-single" => "<!-- twingle --><!-- twingle -->",
"form-single" => "<!-- twingle --><!-- twingle -->",
"eventall" => "<!-- twingle --><!-- twingle -->",
"eventlist" => "<!-- twingle --><!-- twingle -->",
"eventeditcreate" => "<!-- twingle --><!-- twingle -->",
"giftdonationexample" => "https://spenden.twingle.de/gift/my-awesome-ngo/tw555555a555555c/example",
"letterexample" => "https://spenden.twingle.de/letter/my-awesome-ngo/tw555555a555555c/example",
],
"counter-url" => [
"url" => "https://donationstatus.twingle.de/donation-status/555aaa",
],
"host" => "https://spenden.twingle.de/",
"project_options" => [
"id" => 2237,
"has_confirmation_mail" => FALSE,
"has_confirmation_mail_api" => FALSE,
"has_donation_receipt" => TRUE,
"has_contact_data" => FALSE,
"donation_rhythm" => [
"yearly" => TRUE,
"halfyearly" => FALSE,
"quarterly" => TRUE,
"monthly" => TRUE,
"one_time" => TRUE,
],
"default_rhythm" => "",
"has_newsletter_registration" => FALSE,
"has_postinfo_registration" => FALSE,
"design_background_color" => "ffffff",
"design_primary_color" => "102045",
"design_font" => "",
"design_font_color" => "102045",
"design_button_font_color" => "",
"design_button_font_color_light" => "",
"image" => "",
"bcc_email_address" => "mustermann@awesome-ngo.org",
"donation_value_min" => 5,
"donation_value_max" => 1000,
"donation_value_default" => 50,
"exclude_contact_fields" => "company,birthday,telephone",
"mandatory_contact_fields" => "firstname,lastname",
"custom_js" => NULL,
"custom_css" => "https://awesome-ngo.org/css",
"share_url" => "https://awesome-ngo.org/donate",
"has_contact_mandatory" => FALSE,
"has_doi" => TRUE,
"doi_redirect" => NULL,
"has_force_donation_target_buttons" => FALSE,
"has_show_single_target" => FALSE,
"betterpayment_credit_card_theme" => NULL,
"app_js" => NULL,
"slidericon" => "handshake",
"extra_field" => [
],
"has_hidden_logo" => FALSE,
"has_projecttarget_as_money" => FALSE,
"rapidmail_recipient_list" => NULL,
"mailchimp_recipient_list" => NULL,
"has_mailchimp_add_all_user" => NULL,
"has_mail_hide_paymentblock" => FALSE,
"has_mail_hide_paymentblock_api" => FALSE,
"cleverreach_group_id" => NULL,
"cleverreach_form_id" => NULL,
"has_donationtarget_textfield" => FALSE,
"has_civi_crm_activated" => FALSE,
"has_step_index" => FALSE,
"gift_donation" => "",
"gift_donation_icon" => NULL,
"gift_donation_image" => NULL,
"languages" => "de",
"has_buttons" => FALSE,
"has_no_slider" => FALSE,
"buttons" => [
"button1" => [
"amount" => "5",
],
"button2" => [
"amount" => "50",
],
"button3" => [
"amount" => "100",
],
"button4" => [
"amount" => "",
],
],
"has_newsletter_namerequest" => FALSE,
"has_show_donator_data" => FALSE,
"has_donation_letter" => FALSE,
"has_donation_letter_api" => FALSE,
"amount_images" => [],
"video_url" => NULL,
],
"payment_methods" => [
"has_paypal" => TRUE,
"has_betterpayment" => FALSE,
"has_sixpay" => FALSE,
"has_banktransfer" => TRUE,
"has_debit" => TRUE,
"has_sofortueberweisung" => FALSE,
"has_betterpayment_creditcard" => FALSE,
"has_mbe4" => FALSE,
"has_generic" => FALSE,
"has_konfipay" => FALSE,
"has_konfipay_recurring" => FALSE,
"has_betterpayment_creditcard_recurring" => FALSE,
"has_paypal_recurring" => FALSE,
"has_debit_recurring" => TRUE,
"has_generic_recurring" => FALSE,
"has_sixpay_recurring" => FALSE,
"has_apple_pay" => FALSE,
"has_apple_pay_recurring" => FALSE,
"has_paydirekt" => FALSE,
"has_paydirekt_recurring" => FALSE,
],
];