Author: chabotc
Date: Wed May 20 12:22:29 2009
New Revision: 776675

URL: http://svn.apache.org/viewvc?rev=776675&view=rev
Log:
Fixes a parse error of the container.js config and adds a unittest to test for 
it

Modified:
    incubator/shindig/trunk/php/src/gadgets/ContainerConfig.php
    incubator/shindig/trunk/php/src/social/sample/DefaultInvalidateService.php
    incubator/shindig/trunk/php/test/gadgets/ContainerConfigTest.php

Modified: incubator/shindig/trunk/php/src/gadgets/ContainerConfig.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/ContainerConfig.php?rev=776675&r1=776674&r2=776675&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/ContainerConfig.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/ContainerConfig.php Wed May 20 
12:22:29 2009
@@ -48,9 +48,7 @@
 
   private function loadFromFile($file) {
     $contents = file_get_contents($file);
-    // remove all comments (both /* */ and // style) because this confuses the 
json parser
-    // note: the json parser also crashes on trailing ,'s in records so please 
don't use them
-    $contents = preg_replace('/[^http:\/\/|^https:\/\/]\/\/.*$/m', '', 
preg_replace('@/\\*(?:.|[\\n\\r])*?\\*/@', '', $contents));
+    $contents = self::removeComments($contents);
     $config = json_decode($contents, true);
     if ($config == $contents) {
       throw new Exception("Failed to json_decode the container configuration");
@@ -64,6 +62,15 @@
       $this->config[$container][$key] = $val;
     }
   }
+  
+  public static function removeComments($str) {
+    // remove /* */ style comments
+    $str = preg_replace('@/\\*(?:.|[\\n\\r])*?\\*/@', '', $str);
+    // remove // style comments, but keep 'http://' 'https://' and '"//'
+    // for example: "gadgets.oauthGadgetCallbackTemplate" : 
"//%host%/gadgets/oauthcallback"
+    $str = preg_replace('/[^http:\/\/|^https:\/\/|"\/\/]\/\/.*$/m', '', $str);
+    return $str;
+  }
 
   public function getConfig($container, $name) {
     $config = array();

Modified: 
incubator/shindig/trunk/php/src/social/sample/DefaultInvalidateService.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/sample/DefaultInvalidateService.php?rev=776675&r1=776674&r2=776675&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/sample/DefaultInvalidateService.php 
(original)
+++ incubator/shindig/trunk/php/src/social/sample/DefaultInvalidateService.php 
Wed May 20 12:22:29 2009
@@ -134,10 +134,10 @@
       $cached = $this->invalidationEntry->expiredGet($viewerKey);
       $viewerStamp = $cached['found'] ? $cached['data'] : false;
     }
-    if ($ownerStamp) {
+    if (isset($ownerStamp)) {
       $currentInvalidation = $currentInvalidation . 'o=' . $ownerStamp . ';'; 
     }
-    if ($viewerStamp) {
+    if (isset($viewerStamp)) {
       $currentInvalidation = $currentInvalidation . 'v=' . $viewerStamp . ';'; 
     }
     return $currentInvalidation;

Modified: incubator/shindig/trunk/php/test/gadgets/ContainerConfigTest.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/test/gadgets/ContainerConfigTest.php?rev=776675&r1=776674&r2=776675&view=diff
==============================================================================
--- incubator/shindig/trunk/php/test/gadgets/ContainerConfigTest.php (original)
+++ incubator/shindig/trunk/php/test/gadgets/ContainerConfigTest.php Wed May 20 
12:22:29 2009
@@ -22,25 +22,18 @@
  * ContainerConfig test case.
  */
 class ContainerConfigTest extends PHPUnit_Framework_TestCase {
-  
-  /**
-   * @var ContainerConfig
-   */
-  private $ContainerConfig;
 
   /**
    * Prepares the environment before running a test.
    */
   protected function setUp() {
     parent::setUp();
-    $this->ContainerConfig = new 
ContainerConfig(Config::get('container_path'));
-  }
+   }
 
   /**
    * Cleans up the environment after running a test.
    */
   protected function tearDown() {
-    $this->ContainerConfig = null;
     parent::tearDown();
   }
 
@@ -48,7 +41,8 @@
    * Tests ContainerConfig->getConfig()
    */
   public function testGetConfig() {
-    $config = $this->ContainerConfig->getConfig('default', 'gadgets.features');
+    $containerConfig = new ContainerConfig(Config::get('container_path'));
+    $config = $containerConfig->getConfig('default', 'gadgets.features');
     $this->assertArrayHasKey('core.io', $config);
     $this->assertArrayHasKey('views', $config);
     $this->assertArrayHasKey('rpc', $config);
@@ -56,4 +50,33 @@
     $this->assertArrayHasKey('opensocial-0.8', $config);
     $this->assertArrayHasKey('path', $config['opensocial-0.8']);
   }
+  
+  /**
+   * Tests ContainerConfig::removeComments()
+   */
+  public function testRemoveComments() {
+    $jsFile = <<<EOD
+/*
+ * Comments
+ */
+
+// Comments
+{"gadgets.container" : ["default"],
+"gadgets.parent" : null,
+"gadgets.lockedDomainSuffix" : "-a.example.com:8080",
+"gadgets.iframeBaseUri" : "/gadgets/ifr",
+"gadgets.jsUriTemplate" : "http://%host%/gadgets/js/%js%";,
+"gadgets.oauthGadgetCallbackTemplate" : "//%host%/gadgets/oauthcallback"
+}
+EOD;
+    $uncommented = ContainerConfig::removeComments($jsFile);
+    $jsonObj = json_decode($uncommented, true);
+    $this->assertNotEquals($uncommented, $jsonObj);
+    $this->assertEquals(array("default"), $jsonObj["gadgets.container"]);
+    $this->assertEquals(null, $jsonObj["gadgets.parent"]);
+    $this->assertEquals("-a.example.com:8080", 
$jsonObj["gadgets.lockedDomainSuffix"]);
+    $this->assertEquals("/gadgets/ifr", $jsonObj["gadgets.iframeBaseUri"]);
+    $this->assertEquals("http://%host%/gadgets/js/%js%";, 
$jsonObj["gadgets.jsUriTemplate"]);
+    $this->assertEquals("//%host%/gadgets/oauthcallback", 
$jsonObj["gadgets.oauthGadgetCallbackTemplate"]);
+  }
 }


Reply via email to