Fixes to enable UserDashboard tests to all run
authoreileen <emcnaughton@wikimedia.org>
Wed, 28 Nov 2018 02:11:53 +0000 (15:11 +1300)
committereileen <emcnaughton@wikimedia.org>
Wed, 28 Nov 2018 20:13:09 +0000 (09:13 +1300)
tests/phpunit/CRM/Contact/Page/View/UserDashBoardTest.php
tests/phpunit/CRMTraits/Page/PageTestTrait.php
tests/phpunit/api/v3/ContributionPageTest.php

index 395cec106df2a11a81e7d99f47fb1a911bbcd79e..facc2fc7c9e7f1167b618206973ff21a7563056f 100644 (file)
@@ -42,6 +42,13 @@ class CRM_Contact_Page_View_UserDashBoardTest extends CiviUnitTestCase {
    */
   protected $contactID;
 
+  /**
+   * Contributions created for the test.
+   *
+   * @var array
+   */
+  protected $contributions = [];
+
   /**
    * Prepare for test
    */
@@ -58,6 +65,9 @@ class CRM_Contact_Page_View_UserDashBoardTest extends CiviUnitTestCase {
     $this->quickCleanUpFinancialEntities();
     $this->quickCleanup(['civicrm_uf_match']);
     CRM_Utils_Hook::singleton()->reset();
+    CRM_Core_Session::singleton()->reset();
+    CRM_Core_Smarty::singleton()->clearTemplateVars();
+    $this->callAPISuccess('Contact', 'delete', ['id' => $this->contactID]);
   }
 
   /**
@@ -79,42 +89,85 @@ class CRM_Contact_Page_View_UserDashBoardTest extends CiviUnitTestCase {
   /**
    * Test the content of the dashboard.
    */
-  public function testDashboardContentContributions() {
-    $this->contributionCreate(['contact_id' => $this->contactID]);
+  public function testDashboardContentContributionsWithInvoicingEnabled() {
+    $this->contributions[] = $this->contributionCreate([
+      'contact_id' => $this->contactID,
+      'receive_date' => '2018-11-21',
+      'receipt_date' => '2018-11-22',
+    ]);
+    $this->contributions[] = $this->contributionCreate([
+      'contact_id' => $this->contactID,
+      'receive_date' => '2018-11-21',
+      'receipt_date' => '2018-11-22',
+      'trxn_id' => '',
+      'invoice_id' => '',
+    ]);
+    $recur = $this->callAPISuccess('ContributionRecur', 'create', [
+      'contact_id' => $this->contactID,
+      'frequency_interval' => 1,
+      'amount' => 5,
+    ]);
+    $this->contributions[] = $this->contributionCreate([
+      'contact_id' => $this->contactID,
+      'receive_date' => '2018-11-21',
+      'amount_level' => 'high',
+      'contribution_status_id' => 'Cancelled',
+      'invoice_id' => NULL,
+      'trxn_id' => NULL,
+      'contribution_recur_id' => $recur['id'],
+    ]);
+    $this->callAPISuccess('Setting', 'create', ['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>',
-      '</tr><tr id=\'rowid1\'class="odd-row"><td>$ 100.00 </td><td>Donation</td>',
-      '</td><td></td><td>Completed</td></tr></table>',
+      '<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><tr id=\'rowid2\'',
     ];
+
     $this->assertPageContains($expectedStrings);
-    $this->assertSmartyVariables(['invoicing' => NULL]);
+    $this->assertSmartyVariableArrayIncludes('contribute_rows', 0, [
+      'contact_id' => $this->contactID,
+      'contribution_id' => '1',
+      'total_amount' => '100.00',
+      'financial_type' => 'Donation',
+      'contribution_source' => 'SSF',
+      'receive_date' => '2018-11-21 00:00:00',
+      'contribution_status' => 'Completed',
+      'currency' => 'USD',
+      //'receipt_date' => '2018-11-22 00:00:00',
+    ]);
+
   }
 
   /**
    * Test the content of the dashboard.
    */
-  public function testDashboardContentContributionsWithInvoicingEnabled() {
-    $this->markTestIncomplete('some issue on jenkins but not locally - disabling to investigage on master as this is an rc patch');
+  public function testDashboardContentContributions() {
     $this->contributionCreate(['contact_id' => $this->contactID]);
-    $this->callAPISuccess('Setting', 'create', ['invoicing' => 1]);
+    $this->contributions[] = civicrm_api3('Contribution', 'get', [
+      'contact_id' => $this->contactID,
+      'options' => ['limit' => 12, 'sort' => 'receive_date DESC'],
+      'sequential' => 1,
+    ])['values'];
     $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>',
+      '<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>',
+      '<td>$ 100.00 </td><td>Donation</td>',
+      '<td>Completed</td>',
     ];
     $this->assertPageContains($expectedStrings);
-    $this->assertSmartyVariables(['invoicing' => TRUE]);
   }
 
   /**
    * Run the user dashboard.
    */
   protected function runUserDashboard() {
+    $_REQUEST = ['reset' => 1, 'id' => $this->contactID];
     $dashboard = new CRM_Contact_Page_View_UserDashBoard();
+    $dashboard->_contactId = $this->contactID;
     $dashboard->run();
+    $_REQUEST = [];
   }
 
 }
index 150582e7c59084b239e82605ce07f19a97113b72..493369af68d160a7a70f8a5e4a690f4b5c39d725 100644 (file)
@@ -39,6 +39,16 @@ trait CRMTraits_Page_PageTestTrait {
    */
   protected $pageContent;
 
+  /**
+   * @var \CRM_Core_Page
+   */
+  protected $page;
+
+  /**
+   * @var string
+   */
+  protected $tplName;
+
   /**
    * Variables assigned to smarty.
    *
@@ -46,6 +56,8 @@ trait CRMTraits_Page_PageTestTrait {
    */
   protected $smartyVariables = [];
 
+  protected $context;
+
   /**
    * @param string $content
    * @param string $context
@@ -54,6 +66,9 @@ trait CRMTraits_Page_PageTestTrait {
    */
   public function checkPageContent(&$content, $context, $tplName, &$object) {
     $this->pageContent = $content;
+    $this->tplName = $tplName;
+    $this->page = $object;
+    $this->context = $context;
     // Ideally we would validate $content as valid html here.
     // Suppress console output.
     $content = '';
@@ -66,8 +81,10 @@ trait CRMTraits_Page_PageTestTrait {
    * @param $expectedStrings
    */
   protected function assertPageContains($expectedStrings) {
+    unset($this->smartyVariables['config']);
+    unset($this->smartyVariables['session']);
     foreach ($expectedStrings as $expectedString) {
-      $this->assertContains($expectedString, $this->pageContent);
+      $this->assertContains($expectedString, $this->pageContent, print_r($this->contributions, TRUE) . print_r($this->smartyVariables, TRUE));
     }
   }
 
@@ -82,6 +99,23 @@ trait CRMTraits_Page_PageTestTrait {
     }
   }
 
+  /**
+   * Check an array assigned to smarty for the inclusion of the expected variables.
+   *
+   * @param string $variableName
+   * @param $index
+   * @param $expected
+   */
+  protected function assertSmartyVariableArrayIncludes($variableName, $index, $expected) {
+    $smartyVariable = $this->smartyVariables[$variableName];
+    if ($index !== NULL) {
+      $smartyVariable = $smartyVariable[$index];
+    }
+    foreach ($expected as $key => $value) {
+      $this->assertEquals($value, $smartyVariable[$key], 'Checking ' . $key);
+    }
+  }
+
   /**
    * Set up environment to listen for page content.
    */
index 5e7f2479e80e543e6be845d2e1e3daa6b6e4da7a..0a9529957c2eb094bbd0bf44b56e0a8ef27e4b28 100644 (file)
@@ -1798,7 +1798,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $this->callAPISuccess('contribution_page', 'submit', $submitParams);
     $contribution = $this->callAPISuccessGetSingle('contribution', array(
       'contribution_page_id' => $this->_ids['contribution_page'],
-      'contribution_status_id' => 2,
+      'contribution_status_id' => 'Pending',
     ));
     $this->assertEquals(80, $contribution['total_amount']);
     $lineItems = $this->callAPISuccess('LineItem', 'get', array(