Add test for user dashboard
authoreileen <emcnaughton@wikimedia.org>
Wed, 21 Nov 2018 00:47:43 +0000 (13:47 +1300)
committereileen <emcnaughton@wikimedia.org>
Wed, 21 Nov 2018 00:48:07 +0000 (13:48 +1300)
CRM/Contribute/BAO/ContributionRecur.php
tests/phpunit/CRM/Contact/Page/View/UserDashBoardTest.php [new file with mode: 0644]
tests/phpunit/CRMTraits/Page/PageTestTrait.php [new file with mode: 0644]

index 0ac3c9ae246d95c7d79bf29005b8abed621d4c9e..3aebb4b4180ca8b62cd3519798e65343a71474fd 100644 (file)
@@ -795,6 +795,7 @@ INNER JOIN civicrm_contribution       con ON ( con.id = mp.contribution_id )
       '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 (file)
index 0000000..57473d0
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 5                                                  |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2018                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Test class for CRM_Contact_Page_View_UserDashBoard
+ *
+ * @package   CiviCRM
+ * @group headless
+ */
+class CRM_Contact_Page_View_UserDashBoardTest extends CiviUnitTestCase {
+
+  use CRMTraits_Page_PageTestTrait;
+
+  /**
+   * Contact ID of logged in user.
+   *
+   * @var int
+   */
+  protected $contactID;
+
+  /**
+   * Prepare for test
+   */
+  public function setUp() {
+    parent::setUp();
+    $this->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)',
+       '<table class="selector"><tr class="columnheader"><th>Total Amount</th><th>Financial Type</th><th>Received date</th><th>Receipt Sent</th><th>Status</th>',
+      '</tr><tr id=\'rowid1\'class="odd-row"><td>$ 100.00 </td><td>Donation</td>',
+      '</td><td></td><td>Completed</td></tr></table>',
+    ];
+    $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)',
+      '<table class="selector"><tr class="columnheader"><th>Total Amount</th><th>Financial Type</th><th>Received date</th><th>Receipt Sent</th><th>Status</th><th></th>',
+      '<td>Completed</td><td><a class="button no-popup nowrap"href="/index.php?q=civicrm/contribute/invoice&amp;reset=1&amp;id=1&amp;cid=' . $this->contactID . '"><i class="crm-i fa-print"></i><span>Print Invoice</span></a></td></tr></table>',
+    ];
+    $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 (file)
index 0000000..150582e
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 5                                                  |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2018                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Trait CRMTraits_Page_PageTestTrait
+ *
+ * Trait for testing quickform pages in unit tests.
+ */
+trait CRMTraits_Page_PageTestTrait {
+
+  /**
+   * Content from the rendered page.
+   *
+   * @var string
+   */
+  protected $pageContent;
+
+  /**
+   * Variables assigned to smarty.
+   *
+   * @var array
+   */
+  protected $smartyVariables = [];
+
+  /**
+   * @param string $content
+   * @param string $context
+   * @param string $tplName
+   * @param CRM_Core_Page $object
+   */
+  public function checkPageContent(&$content, $context, $tplName, &$object) {
+    $this->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'
+    ]);
+  }
+
+}