From c995333d8bbe6d23b19586031c8b30a9e58be082 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Mon, 27 Jul 2020 08:20:45 +1000 Subject: [PATCH] [NFC] Add in a unit test of calling the contribution page widget endpoint and remove unneded file --- CRM/Widget/Widget.php | 197 ------------------------ tests/phpunit/E2E/Extern/WidgetTest.php | 115 ++++++++++++++ 2 files changed, 115 insertions(+), 197 deletions(-) delete mode 100644 CRM/Widget/Widget.php create mode 100644 tests/phpunit/E2E/Extern/WidgetTest.php diff --git a/CRM/Widget/Widget.php b/CRM/Widget/Widget.php deleted file mode 100644 index dbc4afe80c..0000000000 --- a/CRM/Widget/Widget.php +++ /dev/null @@ -1,197 +0,0 @@ - [ - '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 "......................." . - 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 index 0000000000..99f760c28a --- /dev/null +++ b/tests/phpunit/E2E/Extern/WidgetTest.php @@ -0,0 +1,115 @@ +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' => 'userFrameworkBaseURL . "'" . ' class=\'crm-home-url\' style=\'color:#ffffff\'>Learn more.', + '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); + } + +} -- 2.25.1