[NFC] Add in a unit test of calling the contribution page widget endpoint and remove...
authorSeamus Lee <seamuslee001@gmail.com>
Sun, 26 Jul 2020 22:20:45 +0000 (08:20 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Sun, 26 Jul 2020 23:13:44 +0000 (09:13 +1000)
CRM/Widget/Widget.php [deleted file]
tests/phpunit/E2E/Extern/WidgetTest.php [new file with mode: 0644]

diff --git a/CRM/Widget/Widget.php b/CRM/Widget/Widget.php
deleted file mode 100644 (file)
index dbc4afe..0000000
+++ /dev/null
@@ -1,197 +0,0 @@
-<?php
-/*
- * Copyright (C) 2007 Jacob Singh, Sam Lerner
- * Licensed to CiviCRM under the Academic Free License version 3.0.
- *
- * Modified and improved upon by CiviCRM LLC (c) 2007
- */
-
-/**
- * Class CRM_Widget_Widget
- */
-class CRM_Widget_Widget {
-
-  public static $_methodTable;
-
-  public function initialize() {
-    if (!self::$_methodTable) {
-      self::$_methodTable = [
-        'getContributionPageData' => [
-          'description' => 'Gets all campaign related data and returns it as a std class.',
-          'access' => 'remote',
-          'arguments' => [
-            'contributionPageID',
-            'widgetID',
-          ],
-        ],
-        'getEmbedCode' => [
-          'description' => 'Gets embed code.  Perhaps overkill, but we can track dropoffs in this case. by # of people requesting embed code / number of unique instances.',
-          'access' => 'remote',
-          'arguments' => [
-            'contributionPageID',
-            'widgetID',
-            'format',
-          ],
-        ],
-      ];
-    }
-  }
-
-  public function &methodTable() {
-    self::initialize();
-
-    return self::$_methodTable;
-  }
-
-  /**
-   * Not implemented - registers an action and unique widget ID.  Useful for stats and debugging
-   *
-   * @param int $contributionPageID
-   * @param string $widgetID
-   * @param string $action
-   *
-   * @return string
-   */
-  public function registerRequest($contributionPageID, $widgetID, $action) {
-    return "I registered a request to $action on $contributionPageID from $widgetID";
-  }
-
-  /**
-   * Gets all campaign related data and returns it as a std class.
-   *
-   * @param int $contributionPageID
-   * @param string $widgetID
-   *
-   * @return object
-   */
-  public function getContributionPageData($contributionPageID, $widgetID) {
-    $config = CRM_Core_Config::singleton();
-
-    self::registerRequest($contributionPageID, $widgetID, __FUNCTION__);
-
-    $data = new stdClass();
-
-    if (empty($contributionPageID) ||
-      CRM_Utils_Type::validate($contributionPageID, 'Integer') == NULL
-    ) {
-      $data->is_error = TRUE;
-      CRM_Core_Error::debug_log_message("$contributionPageID is not set");
-      return $data;
-    }
-
-    $widget = new CRM_Contribute_DAO_Widget();
-    $widget->contribution_page_id = $contributionPageID;
-    if (!$widget->find(TRUE)) {
-      $data->is_error = TRUE;
-      CRM_Core_Error::debug_log_message("$contributionPageID is not found");
-      return $data;
-    }
-
-    $data->is_error = FALSE;
-    if (!$widget->is_active) {
-      $data->is_active = FALSE;
-    }
-
-    $data->is_active = TRUE;
-    $data->title = $widget->title;
-    $data->logo = $widget->url_logo;
-    $data->button_title = $widget->button_title;
-    $data->button_url = CRM_Utils_System::url('civicrm/contribute/transact',
-      "reset=1&id=$contributionPageID",
-      TRUE, NULL, FALSE, TRUE
-    );
-    $data->about = $widget->about;
-
-    $query = "
-SELECT count( id ) as count,
-       sum( total_amount) as amount
-FROM   civicrm_contribution
-WHERE  is_test = 0
-AND    contribution_status_id = 1
-AND    contribution_page_id = %1";
-    $params = [1 => [$contributionPageID, 'Integer']];
-    $dao = CRM_Core_DAO::executeQuery($query, $params);
-    if ($dao->fetch()) {
-      $data->num_donors = $dao->count;
-      $data->money_raised = $dao->amount;
-    }
-    else {
-      $data->num_donors = $data->money_raised = 0;
-    }
-
-    $query = "
-SELECT goal_amount, start_date, end_date, is_active
-FROM   civicrm_contribution_page
-WHERE  id = %1";
-    $params = [1 => [$contributionPageID, 'Integer']];
-    $dao = CRM_Core_DAO::executeQuery($query, $params);
-    if ($dao->fetch()) {
-      $data->money_target = $dao->goal_amount;
-      $data->campaign_start = CRM_Utils_Date::customFormat($dao->start_date, $config->dateformatFull);
-      $data->campaign_end = CRM_Utils_Date::customFormat($dao->end_date, $config->dateformatFull);
-
-      // check for time being between start and end date
-      $now = time();
-      if ($dao->start_date) {
-        $startDate = CRM_Utils_Date::unixTime($dao->start_date);
-        if ($startDate &&
-          $startDate >= $now
-        ) {
-          $data->is_active = FALSE;
-        }
-      }
-
-      if ($dao->end_date) {
-        $endDate = CRM_Utils_Date::unixTime($dao->end_date);
-        if ($endDate &&
-          $endDate < $now
-        ) {
-          $data->is_active = FALSE;
-        }
-      }
-    }
-    else {
-      $data->is_active = FALSE;
-    }
-
-    // if is_active is false, show this link and hide the contribute button
-    $data->homepage_link = $widget->url_homepage;
-
-    // movie clip colors, must be in '0xRRGGBB' format
-    $data->colors = [];
-
-    $hexPrefix = '0x';
-    $data->colors["title"] = str_replace('#', $hexPrefix, $widget->color_title);
-    $data->colors["button"] = str_replace('#', $hexPrefix, $widget->color_button);
-    $data->colors["bar"] = str_replace('#', $hexPrefix, $widget->color_bar);
-    $data->colors["main_text"] = str_replace('#', $hexPrefix, $widget->color_main_text);
-    $data->colors["main"] = str_replace('#', $hexPrefix, $widget->color_main);
-    $data->colors["main_bg"] = str_replace('#', $hexPrefix, $widget->color_main_bg);
-    $data->colors["bg"] = str_replace('#', $hexPrefix, $widget->color_bg);
-
-    // these two have colors as normal hex format
-    // because they're being used in a CSS object
-    $data->colors["about_link"] = str_replace('#', $hexPrefix, $widget->color_about_link);
-    $data->colors["homepage_link"] = str_replace('#', $hexPrefix, $widget->color_homepage_link);
-
-    return $data;
-  }
-
-  /**
-   * Gets embed code.  Perhaps overkill, but we can track dropoffs in this case.
-   * by # of people reqeusting emebed code / number of unique instances.
-   *
-   * @param int $contributionPageID
-   * @param string $widgetID
-   * @param string $format
-   *   Either myspace or normal.
-   *
-   * @return string
-   */
-  public function getEmbedCode($contributionPageID, $widgetID, $format = "normal") {
-    self::registerRequest($contributionPageID, $widgetID, __FUNCTION__);
-    return "<embed>.......................</embed>" .
-    print_r(func_get_args(), 1);
-  }
-
-}
diff --git a/tests/phpunit/E2E/Extern/WidgetTest.php b/tests/phpunit/E2E/Extern/WidgetTest.php
new file mode 100644 (file)
index 0000000..99f760c
--- /dev/null
@@ -0,0 +1,115 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Verify Contribution Page Widget endpoint works correctly.
+ * @group e2e
+ */
+class E2E_Extern_WidgetTest extends CiviEndToEndTestCase {
+
+  use \Civi\Test\Api3TestTrait;
+
+  /**
+   * @var string
+   */
+  public $url;
+
+  public function setUp() {
+    $this->url = CRM_Core_Resources::singleton()->getUrl('civicrm', 'extern/widget.php');
+  }
+
+  /**
+   * Return widget Javascript.
+   *
+   */
+  public function testWidget() {
+    $contributionPage = $this->contributionPageCreate();
+    $widgetParams = [
+      'is_active' => 1,
+      'title' => $contributionPage['values'][$contributionPage['id']]['title'],
+      'contribution_page_id' => $contributionPage['id'],
+      'button_title' => 'Contribute!',
+      'color_title' => '#2786c2',
+      'color_button' => '#ffffff',
+      'color_bar' => '#2786c2',
+      'color_main_text' => '#ffffff',
+      'color_main' => '#96c0e7',
+      'color_main_bg' => '#b7e2ff',
+      'color_bg' => '#96c0e7',
+      'color_about_link' => '#556c82',
+      'color_homepage_link' => '#ffffff',
+    ];
+    $widget = new \CRM_Contribute_DAO_Widget();
+    $widget->copyValues($widgetParams);
+    $widget->save();
+    $widget->find(TRUE);
+    $query = ['cpageId' => $contributionPage['id'], 'widgetId' => $widget->id, 'format' => 3];
+    $client = CRM_Utils_HttpClient::singleton();
+    list($status, $data) = $client->post($this->url, $query);
+    $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
+    $check = substr(trim(substr($data, strpos($data, '{'))), 0, -1);
+    $decodedData = json_decode($check, TRUE);
+    $expected = [
+      'currencySymbol' => '$',
+      'is_error' => FALSE,
+      'is_active' => TRUE,
+      'title' => 'Test Contribution Page',
+      'logo' => NULL,
+      'button_title' => 'Contribute!',
+      'about' => NULL,
+      'num_donors' => '0 Donors',
+      'money_raised' => 'Raised $ 0.00 of $ 10,000.00',
+      'money_raised_amount' => '$ 0.00',
+      'campaign_start' => 'Campaign is ongoing',
+      'money_target' => 10000,
+      'money_raised_percentage' => '0%',
+      'money_target_display' => '$ 10,000.00',
+      'money_low' => 0,
+      'home_url' => '<a href=' . "'" . CRM_Core_Config::singleton()->userFrameworkBaseURL . "'" . ' class=\'crm-home-url\' style=\'color:#ffffff\'>Learn more.</a>',
+      'homepage_link' => NULL,
+      'colors' => [
+        'title' => '#2786c2',
+        'button' => '#ffffff',
+        'bar' => '#2786c2',
+        'main_text' => '#ffffff',
+        'main' => '#96c0e7',
+        'main_bg' => '#b7e2ff',
+        'bg' => '#96c0e7',
+        'about_link' => '#556c82',
+      ],
+    ];
+    $this->assertEquals($expected, $decodedData);
+  }
+
+  /**
+   * Create contribution page.
+   *
+   * @param array $params
+   *
+   * @return array
+   *   Array of contribution page
+   */
+  public function contributionPageCreate($params = []) {
+    $this->_pageParams = array_merge([
+      'title' => 'Test Contribution Page',
+      'financial_type_id' => 1,
+      'currency' => 'USD',
+      'financial_account_id' => 1,
+      'is_active' => 1,
+      'is_allow_other_amount' => 1,
+      'min_amount' => 10,
+      'max_amount' => 1000,
+      'goal_amount' => '10000',
+    ], $params);
+    return $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
+  }
+
+}