oldGet = $_GET;
$this->oldPost = $_POST;
$this->token = BasicSecurityToken::createFromValues(1, 1, 1, 'example.com', 'http://example.com/gadget', 1, 1);
}
public function tearDown()
{
unset($_SERVER['HTTP_HOST']);
$_GET = $this->oldGet;
$_POST = $this->oldPost;
}
public function testCreateGadgetFromRawXml()
{
$_GET = array(
'rawxml' => '
Hello, world!
]]>
'
);
$_POST = array();
$context = new GadgetContext('GADGET');
$gadgetFactory = new GadgetFactory($context, $this->token);
$gadget = $gadgetFactory->createGadget();
$this->assertEquals('title', $gadget->gadgetSpec->title);
$this->assertEquals('
Hello, world!
', trim($gadget->gadgetSpec->views['home']['content']));
}
public function testCreateGadgetFromRawXmlInPost()
{
$_POST = array(
'rawxml' => '
Hello, world!
]]>
'
);
$_GET = array();
$context = new GadgetContext('GADGET');
$gadgetFactory = new GadgetFactory($context, $this->token);
$gadget = $gadgetFactory->createGadget();
$this->assertEquals('title', $gadget->gadgetSpec->title);
$this->assertEquals('Hello, world!
', trim($gadget->gadgetSpec->views['home']['content']));
}
public function testParseFeaturesDependentOnCurrentView() {
$_POST = array(
'rawxml' => '
'
);
$_GET = array();
$context = new GadgetContext('GADGET');
$context->setView('profile');
$gadgetFactory = new GadgetFactory($context, $this->token);
$gadget = $gadgetFactory->createGadget();
$this->assertTrue(in_array('flash', $gadget->features));
$this->assertTrue(in_array('minimessage', $gadget->features));
$this->assertTrue(in_array('opensocial-data', $gadget->features));
$this->assertFalse(in_array('pubsub', $gadget->features));
$this->assertFalse(in_array('pubsub2', $gadget->features));
}
public function testRequiringInvalidFeatureThrowsException() {
$this->setExpectedException('apache\shindig\gadgets\GadgetException', 'Unknown features: invalid');
$_POST = array(
'rawxml' => '
'
);
$_GET = array();
$context = new GadgetContext('GADGET');
$context->setView('profile');
$gadgetFactory = new GadgetFactory($context, $this->token);
$gadget = $gadgetFactory->createGadget();
}
public function testParsePreloadsDependentOnCurrentView() {
$_POST = array(
'rawxml' => '
'
);
$_GET = array();
$context = new GadgetContext('GADGET');
$context->setView('profile');
$gadgetFactory = new TestGadgetFactory($context, $this->token);
$gadget = $gadgetFactory->createGadget();
$this->assertEquals('http://www.example.com/one', $gadget->gadgetSpec->preloads[0]['id']);
$this->assertEquals('http://www.example.com/three', $gadget->gadgetSpec->preloads[1]['id']);
$this->assertEquals(2, count($gadget->gadgetSpec->preloads));
}
public function testParseLocalsDependentOnCurrentView() {
$_POST = array(
'rawxml' => '
'
);
$_GET = array();
$context = new GadgetContext('GADGET');
$context->setView('profile');
$gadgetFactory = new TestGadgetFactory($context, $this->token);
$gadget = $gadgetFactory->createGadget();
$this->assertEquals(array('greetingOne' => 'Hello', 'greetingThree' => 'Hello'), $gadget->gadgetSpec->locales);
}
}
class TestGadgetFactory extends GadgetFactory
{
private $responses = array(
'http://www.example.com/one' => 'preloadOne',
'http://www.example.com/two' => 'preloadTwo',
'http://www.example.com/three' => 'preloadThree',
'http://example.com/helloOne/en_ALL.xml' => '
Hello
',
'http://example.com/helloTwo/en_ALL.xml' => '
Hello
',
'http://example.com/helloThree/en_ALL.xml' => '
Hello
',
);
/**
* mock request sending
*
* @param array $unsignedRequests
* @param array $signedRequests
* @return array
*/
protected function performRequests($unsignedRequests, $signedRequests) {
// Perform the non-signed requests
$responses = array();
if (count($unsignedRequests)) {
foreach ($unsignedRequests as $request) {
$responses[$request->getUrl()] = array(
'body' => $this->responses[$request->getUrl()],
'rc' => 200);
}
}
// Perform the signed requests
if (count($signedRequests)) {
foreach ($signedRequests as $request) {
$responses[$request->getUrl()] = array(
'body' => $this->responses[$request->getUrl()],
'rc' => 200);
}
}
return $responses;
}
}