Improve unit test on rendered urls
authoreileen <emcnaughton@wikimedia.org>
Mon, 3 May 2021 04:18:06 +0000 (16:18 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 17 May 2021 02:09:15 +0000 (14:09 +1200)
tests/phpunit/CRM/Contribute/Page/TabTest.php

index 722e6fdc315855cee65a3d306616e77d9be4b8f6..f06faf1f5d393b77980abc4b698a50c4a09534b5 100644 (file)
@@ -18,22 +18,77 @@ use Civi\Api4\ContributionRecur;
  */
 class CRM_Contribute_Page_TabTest extends CiviUnitTestCase {
 
+  /**
+   * Clean up after test.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function tearDown(): void {
+    $this->quickCleanUpFinancialEntities();
+    parent::tearDown();
+  }
+
   /**
    * Test links render correctly for manual processor.
    *
    * @throws \API_Exception
    * @throws \CiviCRM_API3_Exception
    */
-  public function testLinks() {
-    $contactID = $this->individualCreate();
-    $recurID = ContributionRecur::create()->setValues([
+  public function testLinksManual(): void {
+    [$contactID, $recurID] = $this->setupTemplate();
+
+    $templateVariable = CRM_Core_Smarty::singleton()->get_template_vars();
+    $this->assertEquals('Mr. Anthony Anderson II', $templateVariable['displayName']);
+    $this->assertEquals("<span><a href=\"/index.php?q=civicrm/contact/view/contributionrecur&amp;reset=1&amp;id=" . $recurID . "&amp;cid=" . $contactID . "&amp;context=contribution\" class=\"action-item crm-hover-button\" title='View Recurring Payment' >View</a><a href=\"/index.php?q=civicrm/contribute/updaterecur&amp;reset=1&amp;action=update&amp;crid=1&amp;cid=3&amp;context=contribution\" class=\"action-item crm-hover-button\" title='Edit Recurring Payment' >Edit</a><a href=\"/index.php?q=civicrm/contribute/unsubscribe&amp;reset=1&amp;crid=" . $recurID . "&amp;cid=" . $contactID . "&amp;context=contribution\" class=\"action-item crm-hover-button\" title='Cancel' >Cancel</a></span>",
+      $this->getActionHtml()
+    );
+  }
+
+  /**
+   * Test links render correctly for manual processor.
+   *
+   * @throws \API_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testLinksPaypalStandard(): void {
+    $this->setupTemplate([
+      'payment_processor_id' => $this->paymentProcessorCreate(['payment_processor_type_id' => 'PayPal_Standard']),
+      'contact_id' => $this->createLoggedInUser(),
+    ]);
+    $expected = '<span><a href="/index.php?q=civicrm/contact/view/contributionrecur&amp;reset=1&amp;id=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'View Recurring Payment\' >View</a><a href="/index.php?q=civicrm/contribute/updaterecur&amp;reset=1&amp;action=update&amp;crid=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'Edit Recurring Payment\' >Edit</a></span><span class=\'btn-slide crm-hover-button\'>more<ul class=\'panel\'><li><a href="/index.php?q=civicrm/contribute/unsubscribe&amp;reset=1&amp;crid=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'Cancel\' >Cancel</a></li><li><a href="/index.php?q=civicrm/contribute/updatebilling&amp;reset=1&amp;crid=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'Change Billing Details\' >Change Billing Details</a></li></ul></span>';
+    $this->assertEquals($expected, $this->getActionHtml());
+
+    $page = new CRM_Contribute_Page_UserDashboard();
+    $page->run();
+    $expected = '<span><a href="/index.php?q=civicrm/contact/view/contributionrecur&amp;reset=1&amp;id=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'View Recurring Payment\' >View</a><a href="/index.php?q=civicrm/contribute/updaterecur&amp;reset=1&amp;action=update&amp;crid=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'Edit Recurring Payment\' >Edit</a></span><span class=\'btn-slide crm-hover-button\'>more<ul class=\'panel\'><li><a href="/index.php?q=civicrm/contribute/unsubscribe&amp;reset=1&amp;crid=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'Cancel\' >Cancel</a></li><li><a href="/index.php?q=civicrm/contribute/updatebilling&amp;reset=1&amp;crid=1&amp;cid=3&amp;context=contribution" class="action-item crm-hover-button" title=\'Change Billing Details\' >Change Billing Details</a></li></ul></span>';
+    $this->assertEquals(
+      $expected,
+      $this->getActionHtml()
+    );
+  }
+
+  /**
+   * Set up template for user dashboard.
+   *
+   * Create the recurring contribution, contribution and run the dashboard.
+   *
+   * @param array $recurParams
+   *
+   * @return array
+   * @throws \API_Exception
+   * @throws \CiviCRM_API3_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
+   */
+  protected function setupTemplate($recurParams = []): array {
+    $contactID = $recurParams['contact_id'] ?? $this->individualCreate();
+    $recurID = ContributionRecur::create()->setValues(array_merge([
       'contact_id' => $contactID,
       'amount' => 10,
       'frequency_interval' => 'week',
       'start_date' => 'now',
       'is_active' => TRUE,
       'contribution_status_id:name' => 'Pending',
-    ])
+    ], $recurParams))
       ->addChain(
         'contribution',
         Contribution::create()->setValues([
@@ -48,12 +103,17 @@ class CRM_Contribute_Page_TabTest extends CiviUnitTestCase {
     $page->_contactId = $contactID;
     $page->_action = CRM_Core_Action::VIEW;
     $page->browse();
+    return [$contactID, $recurID];
+  }
 
-    $templateVariable = CRM_Core_Smarty::singleton()->get_template_vars();
-    $this->assertEquals('Mr. Anthony Anderson II', $templateVariable['displayName']);
-    $this->assertEquals("<span><a href=\"/index.php?q=civicrm/contact/view/contributionrecur&amp;reset=1&amp;id=" . $recurID . "&amp;cid=" . $contactID . "&amp;context=contribution\" class=\"action-item crm-hover-button\" title='View Recurring Payment' >View</a><a href=\"/index.php?q=civicrm/contribute/updaterecur&amp;reset=1&amp;action=update&amp;crid=1&amp;cid=3&amp;context=contribution\" class=\"action-item crm-hover-button\" title='Edit Recurring Payment' >Edit</a><a href=\"/index.php?q=civicrm/contribute/unsubscribe&amp;reset=1&amp;crid=" . $recurID . "&amp;cid=" . $contactID . "&amp;context=contribution\" class=\"action-item crm-hover-button\" title='Cancel' >Cancel</a></span>",
-      $templateVariable['activeRecurRows'][1]['action']
-    );
+  /**
+   * Get the html assigned as actions.
+   *
+   * @return string
+   */
+  protected function getActionHtml(): string {
+    return CRM_Core_Smarty::singleton()
+      ->get_template_vars()['activeRecurRows'][1]['action'];
   }
 
 }