Skip to content

Commit

Permalink
dev/cloud-native#16 Migrate Contribution Page widget extern url to us…
Browse files Browse the repository at this point in the history
…e the normal CMS routing mechanism

Update test to test both endpoints
  • Loading branch information
seamuslee001 committed Sep 30, 2020
1 parent ee523fb commit a38da21
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 62 deletions.
51 changes: 51 additions & 0 deletions CRM/Contribute/Page/Widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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 |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
class CRM_Contribute_Page_Widget extends CRM_Core_Page {

/**
* the main function that is called when the page
* loads, it decides the which action has to be taken for the page.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function run() {
$config = CRM_Core_Config::singleton();
$template = CRM_Core_Smarty::singleton();

$cpageId = CRM_Utils_Request::retrieve('cpageId', 'Positive');
$widgetId = CRM_Utils_Request::retrieve('widgetId', 'Positive');
$format = CRM_Utils_Request::retrieve('format', 'Positive');
$includePending = CRM_Utils_Request::retrieve('includePending', 'Boolean');

$jsonvar = 'jsondata';
if (isset($format)) {
$jsonvar .= $cpageId;
}

$data = CRM_Contribute_BAO_Widget::getContributionPageData($cpageId, $widgetId, $includePending);

$output = '
var ' . $jsonvar . ' = ' . json_encode($data) . ';
';

CRM_Core_Page_AJAX::setJsHeaders(60);
echo $output;
CRM_Utils_System::civiExit();
}

}
8 changes: 8 additions & 0 deletions CRM/Contribute/xml/Menu/Contribute.xml
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,12 @@
<page_callback>CRM_Member_Page_RecurringContributions</page_callback>
<access_arguments>access CiviContribute</access_arguments>
</item>
<item>
<path>civicrm/contribute/widget</path>
<title>CiviContribute</title>
<page_callback>CRM_Contribute_Page_Widget</page_callback>
<access_callback>1</access_callback>
<weight>0</weight>
<is_public>true</is_public>
</item>
</menu>
4 changes: 4 additions & 0 deletions CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ public static function migrateExternUrl(\Civi\Core\Event\GenericHookEvent $e) {
$e->url = $mkRouteUri('civicrm/mailing/url', $e->query);
break;

case 'router:extern/widget':
$e->url = $mkRouteUri('civicrm/contribute/widget', $e->query);
break;

// Otherwise, keep the default.
}
}
Expand Down
2 changes: 1 addition & 1 deletion sql/civicrm_generated.mysql

Large diffs are not rendered by default.

124 changes: 63 additions & 61 deletions tests/phpunit/E2E/Extern/WidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,71 +22,73 @@ class E2E_Extern_WidgetTest extends CiviEndToEndTestCase {
*/
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);
if (CIVICRM_UF !== 'Drupal8') {
$endpoints['traditional'] = CRM_Core_Resources::singleton()->getUrl('civicrm', 'extern/widget.php');
}
$endpoints['normal'] = CRM_Utils_System::url('civicrm/contribute/widget', NULL, TRUE, NULL, FALSE, TRUE);
foreach ($endpoints as $key => $url) {
$this->url = $url;
$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, 'Data not matched for endpoint ' . $key);
}
}

/**
Expand Down

0 comments on commit a38da21

Please sign in to comment.