From 98867798423cc5ec3307df281d6a38f40c68505d Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 21 Nov 2018 13:47:43 +1300 Subject: [PATCH] Add test for user dashboard --- CRM/Contribute/BAO/ContributionRecur.php | 1 + .../Contact/Page/View/UserDashBoardTest.php | 112 ++++++++++++++++++ .../phpunit/CRMTraits/Page/PageTestTrait.php | 95 +++++++++++++++ 3 files changed, 208 insertions(+) create mode 100644 tests/phpunit/CRM/Contact/Page/View/UserDashBoardTest.php create mode 100644 tests/phpunit/CRMTraits/Page/PageTestTrait.php diff --git a/CRM/Contribute/BAO/ContributionRecur.php b/CRM/Contribute/BAO/ContributionRecur.php index 0ac3c9ae246d..3aebb4b4180c 100644 --- a/CRM/Contribute/BAO/ContributionRecur.php +++ b/CRM/Contribute/BAO/ContributionRecur.php @@ -795,6 +795,7 @@ public static function recurringContribution(&$form) { 'return' => ["id", "name", 'is_test'], ]; $paymentProcessors = civicrm_api3('PaymentProcessor', 'get', $paymentProcessorParams); + $paymentProcessorOpts = []; foreach ($paymentProcessors['values'] as $key => $value) { $paymentProcessorOpts[$key] = $value['name'] . ($value['is_test'] ? ' (Test)' : ''); } diff --git a/tests/phpunit/CRM/Contact/Page/View/UserDashBoardTest.php b/tests/phpunit/CRM/Contact/Page/View/UserDashBoardTest.php new file mode 100644 index 000000000000..57473d09ba21 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Page/View/UserDashBoardTest.php @@ -0,0 +1,112 @@ +contactID = $this->createLoggedInUser(); + $this->listenForPageContent(); + } + + /** + * Test the content of the dashboard. + */ + public function testDashboardContentEmptyContact() { + $this->runUserDashboard(); + $expectedStrings = [ + 'You are not currently subscribed to any Groups', + 'There are no contributions on record for you.', + 'There are no Pledges for your record.', + 'You are not registered for any current or upcoming Events.', + 'There are no memberships on record for you.', + 'You do not have any active Personal Campaign pages.', + ]; + $this->assertPageContains($expectedStrings); + } + + /** + * Test the content of the dashboard. + */ + public function testDashboardContentContributions() { + $this->contributionCreate(['contact_id' => $this->contactID]); + $this->runUserDashboard(); + $expectedStrings = [ + 'Your Contribution(s)', + '', + '', + '
Total AmountFinancial TypeReceived dateReceipt SentStatus
$ 100.00 DonationCompleted
', + ]; + $this->assertPageContains($expectedStrings); + $this->assertSmartyVariables(['invoicing' => NULL]); + } + + /** + * Test the content of the dashboard. + */ + public function testDashboardContentContributionsWithInvoicingEnabled() { + $this->contributionCreate(['contact_id' => $this->contactID]); + $this->callAPISuccess('Setting', 'create', ['invoicing' => 1, 'contribution_invoice_settings' => [ + 'invoicing' => 1, + ]]); + $this->runUserDashboard(); + $expectedStrings = [ + 'Your Contribution(s)', + '', + '
Total AmountFinancial TypeReceived dateReceipt SentStatusCompletedPrint Invoice
', + ]; + $this->assertPageContains($expectedStrings); + $this->assertSmartyVariables(['invoicing' => TRUE]); + } + + /** + * Run the user dashboard. + */ + protected function runUserDashboard() { + $dashboard = new CRM_Contact_Page_View_UserDashBoard(); + $dashboard->run(); + } + +} diff --git a/tests/phpunit/CRMTraits/Page/PageTestTrait.php b/tests/phpunit/CRMTraits/Page/PageTestTrait.php new file mode 100644 index 000000000000..150582e7c590 --- /dev/null +++ b/tests/phpunit/CRMTraits/Page/PageTestTrait.php @@ -0,0 +1,95 @@ +pageContent = $content; + // Ideally we would validate $content as valid html here. + // Suppress console output. + $content = ''; + $this->smartyVariables = CRM_Core_Smarty::singleton()->get_template_vars(); + } + + /** + * Assert that the page output contains the expected strings. + * + * @param $expectedStrings + */ + protected function assertPageContains($expectedStrings) { + foreach ($expectedStrings as $expectedString) { + $this->assertContains($expectedString, $this->pageContent); + } + } + + /** + * Assert that the expected variables have been assigned to Smarty. + * + * @param $expectedVariables + */ + protected function assertSmartyVariables($expectedVariables) { + foreach ($expectedVariables as $variableName => $expectedValue) { + $this->assertEquals($expectedValue, $this->smartyVariables[$variableName]); + } + } + + /** + * Set up environment to listen for page content. + */ + protected function listenForPageContent() { + $this->hookClass->setHook('civicrm_alterContent', [ + $this, + 'checkPageContent' + ]); + } + +}