Author: bhofmann
Date: Tue Aug 17 09:55:28 2010
New Revision: 986258
URL: http://svn.apache.org/viewvc?rev=986258&view=rev
Log:
SHINDIG-1139: Parameter substitution in GadgetUrlRenderer for redirect URL
Original Patch from Dmitry Vorobyev
+ Adaption to current code base and unit test
Added:
shindig/trunk/php/test/gadgets/GadgetUrlRendererTest.php
Modified:
shindig/trunk/php/src/gadgets/render/GadgetUrlRenderer.php
shindig/trunk/php/test/gadgets/GadgetHtmlRendererTest.php
Modified: shindig/trunk/php/src/gadgets/render/GadgetUrlRenderer.php
URL:
http://svn.apache.org/viewvc/shindig/trunk/php/src/gadgets/render/GadgetUrlRenderer.php?rev=986258&r1=986257&r2=986258&view=diff
==============================================================================
--- shindig/trunk/php/src/gadgets/render/GadgetUrlRenderer.php (original)
+++ shindig/trunk/php/src/gadgets/render/GadgetUrlRenderer.php Tue Aug 17
09:55:28 2010
@@ -30,29 +30,46 @@ class GadgetUrlRenderer extends GadgetRe
* @param Array $view
*/
public function renderGadget(Gadget $gadget, $view) {
+ $redirURI = $this->getSubstitutedUrl($gadgt, $view);
+ header('Location: ' . $redirURI);
+ }
+
+ /**
+ * retrieves url of content tag and substitutes it
+ *
+ * @param Gadget $gadget
+ * @param string $view
+ * @return string
+ */
+ public function getSubstitutedUrl(Gadget $gadget, $view) {
// Preserve existing query string parameters.
$redirURI = $view['href'];
- $queryStr = strpos($redirURI, '?') !== false ? substr($redirURI,
strpos($redirURI, '?')) : '';
- $query = $queryStr;
- $query .= $this->getPrefsQueryString($gadget->gadgetSpec->userPrefs);
-
+ $query = $this->getPrefsQueryString($gadget->gadgetSpec->userPrefs);
+
// deal with features
$registry = $this->context->getRegistry();
// since the URL mode doesn't actually have the gadget XML body, it can't
inline
// the javascript content anyway - thus could us just ignore the
'forcedJsLibs' part.
$sortedFeatures = array();
$registry->sortFeatures($gadget->features, $sortedFeatures);
-
+
$query .= $this->appendLibsToQuery($sortedFeatures);
$query .= '&lang=' . urlencode(isset($_GET['lang']) ? $_GET['lang'] :
'en');
$query .= '&country=' . urlencode(isset($_GET['country']) ?
$_GET['country'] : 'US');
- if (substr($query, 0, 1) == '&') {
- $query = '?' . substr($query, 1);
+
+ $redirURI = $gadget->substitutions->substituteUri(null, $redirURI);
+ if (strpos($redirURI, '?') !== false) {
+ $redirURI = $redirURI . $query;
+ } elseif (substr($query, 0, 1) == '&') {
+ $redirURI = $redirURI . '?' . substr($query, 1);
+ } else {
+ $redirURI = $redirURI . '?' . $query;
}
- $redirURI .= $query;
- header('Location: ' . $redirURI);
+
+ return $redirURI;
}
+
/**
* Returns the requested libs (from getjsUrl) with the libs_param_name
prepended
* ie: in libs=core:caja:etc.js format
Modified: shindig/trunk/php/test/gadgets/GadgetHtmlRendererTest.php
URL:
http://svn.apache.org/viewvc/shindig/trunk/php/test/gadgets/GadgetHtmlRendererTest.php?rev=986258&r1=986257&r2=986258&view=diff
==============================================================================
--- shindig/trunk/php/test/gadgets/GadgetHtmlRendererTest.php (original)
+++ shindig/trunk/php/test/gadgets/GadgetHtmlRendererTest.php Tue Aug 17
09:55:28 2010
@@ -86,7 +86,7 @@ class GadgetHtmlRendererTest extends PHP
$this->gadget = $gadgetSpecFactory->createGadget();
// init gadgetRenderer;
- $this->GadgetHtmlRenderer = new GadgetHtmlRenderer($this->gadgetContext);
+ $this->gadgetHtmlRenderer = new GadgetHtmlRenderer($this->gadgetContext);
// init $this->doc
$this->domDocument = new DOMDocument(null, 'utf-8');
@@ -105,7 +105,7 @@ class GadgetHtmlRendererTest extends PHP
protected function tearDown() {
$this->gadget = null;
$this->gadgetContext = null;
- $this->GadgetHtmlRenderer = null;
+ $this->gadgetHtmlRenderer = null;
$this->view = null;
$this->domDocument = null;
$this->domElement = null;
@@ -118,7 +118,7 @@ class GadgetHtmlRendererTest extends PHP
*/
public function testRenderGadget() {
ob_start();
- $this->GadgetHtmlRenderer->renderGadget($this->gadget, $this->view);
+ $this->gadgetHtmlRenderer->renderGadget($this->gadget, $this->view);
ob_end_clean();
}
@@ -126,7 +126,7 @@ class GadgetHtmlRendererTest extends PHP
* Tests GadgetHtmlRenderer->addBodyTags()
*/
public function testAddBodyTags() {
- $this->GadgetHtmlRenderer->addBodyTags($this->domElement,
$this->domDocument);
+ $this->gadgetHtmlRenderer->addBodyTags($this->domElement,
$this->domDocument);
$tmpNodeList = $this->domElement->getElementsByTagName("script");
foreach($tmpNodeList as $tmpNode) {
$this->assertEquals('gadgets.util.runOnLoadHandlers();',
$tmpNode->nodeValue);
@@ -138,13 +138,13 @@ class GadgetHtmlRendererTest extends PHP
*/
public function testAddHeadTags() {
ob_start();
- $this->GadgetHtmlRenderer->renderGadget($this->gadget, $this->view);
+ $this->gadgetHtmlRenderer->renderGadget($this->gadget, $this->view);
ob_end_clean();
- $this->GadgetHtmlRenderer->addHeadTags($this->domElement,
$this->domDocument);
+ $this->gadgetHtmlRenderer->addHeadTags($this->domElement,
$this->domDocument);
// TODO: currently we just test the script part
$tmpNodeList = $this->domElement->getElementsByTagName("script");
- $scripts = $this->GadgetHtmlRenderer->getJavaScripts();
+ $scripts = $this->gadgetHtmlRenderer->getJavaScripts();
$idx = 0;
foreach($tmpNodeList as $tmpNode) {
Added: shindig/trunk/php/test/gadgets/GadgetUrlRendererTest.php
URL:
http://svn.apache.org/viewvc/shindig/trunk/php/test/gadgets/GadgetUrlRendererTest.php?rev=986258&view=auto
==============================================================================
--- shindig/trunk/php/test/gadgets/GadgetUrlRendererTest.php (added)
+++ shindig/trunk/php/test/gadgets/GadgetUrlRendererTest.php Tue Aug 17
09:55:28 2010
@@ -0,0 +1,114 @@
+<?php
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+class MockUrlGadgetFactory extends GadgetFactory {
+ public function __construct(GadgetContext $context, $token) {
+ parent::__construct($context, $token);
+ }
+
+ protected function fetchGadget($gadgetUrl) {
+ return '<?xml version="1.0" encoding="UTF-8" ?>
+<Module>
+ <ModulePrefs title="title">
+ <Require feature="opensocial-0.8" />
+ <Require feature="dynamic-height" />
+ </ModulePrefs>
+ <UserPref name="key" datatype="string" default_value="value" />
+ <Content type="url" url="http://example.com/gadget.php" view="home">
+ </Content>
+</Module>';
+ }
+}
+
+/**
+ * GadgetUrlRendererTest test case.
+ */
+class GadgetUrlRendererTest extends PHPUnit_Framework_TestCase {
+
+ /**
+ * @var Gadget
+ */
+ private $gadget;
+
+ /**
+ * @var GadgetContext
+ */
+ private $gadgetContext;
+
+ /**
+ * @var GadgetHtmlRender
+ */
+ private $gadgetUrlRenderer;
+
+
+ protected function setUp() {
+ parent::setUp();
+ $this->gadgetContext = new GadgetContext('GADGET');
+ $gadgetSpecFactory = new MockUrlGadgetFactory($this->gadgetContext, null);
+ $gadgetSpecFactory->fetchGadget = null;
+ $this->gadget = $gadgetSpecFactory->createGadget();
+ $this->gadgetUrlRenderer = new GadgetUrlRenderer($this->gadgetContext);
+ }
+
+ public function testGetUrl() {
+ $view = array(
+ 'href' => 'http://example.com/gadget.php',
+ );
+ $redirectUri = $this->gadgetUrlRenderer->getSubstitutedUrl($this->gadget,
$view);
+ $parsedUrl = parse_url($redirectUri);
+ $queryParameters = array();
+ parse_str($parsedUrl['query'], $queryParameters);
+ $this->assertEquals('example.com', $parsedUrl['host']);
+ $this->assertEquals('/gadget.php', $parsedUrl['path']);
+ $this->assertEquals('dynamic-height:opensocial-0.8.js',
$queryParameters['libs']);
+ $this->assertEquals('en', $queryParameters['lang']);
+ $this->assertEquals('US', $queryParameters['country']);
+ $this->assertEquals('value',$queryParameters['up_key']);
+ }
+
+ public function testGetSubstitutedUrl() {
+ $view = array(
+ 'href' => 'http://example.com/gadget.php?foo=bar&mid=__MODULE_ID__',
+ );
+ $redirectUri = $this->gadgetUrlRenderer->getSubstitutedUrl($this->gadget,
$view);
+ $parsedUrl = parse_url($redirectUri);
+ $queryParameters = array();
+ parse_str($parsedUrl['query'], $queryParameters);
+ $this->assertEquals('example.com', $parsedUrl['host']);
+ $this->assertEquals('/gadget.php', $parsedUrl['path']);
+ $this->assertEquals('dynamic-height:opensocial-0.8.js',
$queryParameters['libs']);
+ $this->assertEquals('en', $queryParameters['lang']);
+ $this->assertEquals('US', $queryParameters['country']);
+ $this->assertEquals('bar',$queryParameters['foo']);
+ $this->assertEquals('value',$queryParameters['up_key']);
+ $this->assertEquals('0',$queryParameters['mid']);
+ }
+
+ /**
+ * Cleans up the environment after running a test.
+ */
+ protected function tearDown() {
+ $this->gadget = null;
+ $this->gadgetContext = null;
+ $this->gadgetUrlRenderer = null;
+
+ parent::tearDown();
+ }
+}