diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0f9f25d..af5fa8c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,7 @@ - + - + ./tests/phpunit diff --git a/tests/phpunit/CRM/TwingleCampaign/BAO/TwingleProjectTest.php b/tests/phpunit/CRM/TwingleCampaign/BAO/TwingleProjectTest.php new file mode 100644 index 0000000..bf82a4b --- /dev/null +++ b/tests/phpunit/CRM/TwingleCampaign/BAO/TwingleProjectTest.php @@ -0,0 +1,142 @@ +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.' + ); + } +} + diff --git a/tests/resources/twingle_project_bad_dummy.php b/tests/resources/twingle_project_bad_dummy.php new file mode 100644 index 0000000..a6aeda6 --- /dev/null +++ b/tests/resources/twingle_project_bad_dummy.php @@ -0,0 +1,136 @@ + 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" => "", + "form" => "", + "widget-single" => "", + "form-single" => "", + "eventall" => "", + "eventlist" => "", + "eventeditcreate" => "", + "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, + ], +]; + diff --git a/tests/resources/twingle_project_dummy.php b/tests/resources/twingle_project_dummy.php new file mode 100644 index 0000000..760f52b --- /dev/null +++ b/tests/resources/twingle_project_dummy.php @@ -0,0 +1,137 @@ + 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" => "", + "form" => "", + "widget-single" => "", + "form-single" => "", + "eventall" => "", + "eventlist" => "", + "eventeditcreate" => "", + "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, + ], +]; +