Example #1
0
 /**
  * @param CM_Model_User|string|null $recipient
  * @param array|null                $tplParams
  * @param CM_Site_Abstract|null     $site
  * @throws CM_Exception_Invalid
  */
 public function __construct($recipient = null, array $tplParams = null, CM_Site_Abstract $site = null)
 {
     if ($this->hasTemplate()) {
         $this->setRenderLayout(true);
     }
     if ($tplParams) {
         foreach ($tplParams as $key => $value) {
             $this->setTplParam($key, $value);
         }
     }
     if (!is_null($recipient)) {
         if (is_string($recipient)) {
             $this->addTo($recipient);
         } elseif ($recipient instanceof CM_Model_User) {
             $this->_recipient = $recipient;
             $this->addTo($this->_recipient->getEmail());
             $this->setTplParam('recipient', $recipient);
         } else {
             throw new CM_Exception_Invalid('Invalid Recipient defined.');
         }
     }
     if (!$site && $this->_recipient) {
         $site = $this->_recipient->getSite();
     }
     if (!$site) {
         $site = CM_Site_Abstract::factory();
     }
     $this->_site = $site;
     $this->setTplParam('siteName', $this->_site->getName());
     $this->setSender($this->_site->getEmailAddress(), $this->_site->getName());
 }
Example #2
0
    public function testTrackPageView()
    {
        $viewer = CMTest_TH::createUser();
        $environment = new CM_Frontend_Environment(CM_Site_Abstract::factory(), $viewer);
        $client = new CMService_AdWords_Client();
        $pushConversion = new ReflectionMethod($client, '_pushConversion');
        $pushConversion->setAccessible(true);
        $pushConversion->invoke($client, $viewer, CMService_AdWords_Conversion::fromJson('{"google_conversion_id":123456,"google_conversion_language":"en","google_conversion_format":"1","google_conversion_color":"666666","google_conversion_label":"label","google_remarketing_only":true,"google_conversion_value":123,"google_conversion_currency":"USD","google_custom_params":{"a":1,"b":2}}'));
        $pushConversion->invoke($client, $viewer, CMService_AdWords_Conversion::fromJson('{"google_conversion_id":789}'));
        $this->assertSame('', $client->getJs());
        $this->assertSame(<<<EOD
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion_async.js" charset="utf-8"></script>
EOD
, $client->getHtml($environment));
        $client->trackPageView($environment, '/');
        $this->assertSame(<<<EOD
window.google_trackConversion({"google_conversion_id":123456,"google_conversion_language":"en","google_conversion_format":"1","google_conversion_color":"666666","google_conversion_label":"label","google_remarketing_only":true,"google_conversion_value":123,"google_conversion_currency":"USD","google_custom_params":{"a":1,"b":2}});window.google_trackConversion({"google_conversion_id":789});
EOD
, $client->getJs());
        $this->assertSame(<<<EOD
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion_async.js" charset="utf-8"></script><script type="text/javascript">
/* <![CDATA[ */
window.google_trackConversion({"google_conversion_id":123456,"google_conversion_language":"en","google_conversion_format":"1","google_conversion_color":"666666","google_conversion_label":"label","google_remarketing_only":true,"google_conversion_value":123,"google_conversion_currency":"USD","google_custom_params":{"a":1,"b":2}});window.google_trackConversion({"google_conversion_id":789});
//]]>
</script>
EOD
, $client->getHtml($environment));
    }
Example #3
0
 public function testProcessTranslationsEnableKey()
 {
     $language = CMTest_TH::createLanguage('test');
     $languageKey = CM_Model_LanguageKey::create('Hello World');
     $this->assertSame(false, $languageKey->getJavascript());
     $site = CM_Site_Abstract::factory();
     $render = new CM_Frontend_Render(new CM_Frontend_Environment(null, null, $language));
     // Check that key is *not* included in the JS translations list
     $versionJavascript = CM_Model_Language::getVersionJavascript();
     $request = new CM_Http_Request_Get($render->getUrlResource('library-js', "translations/{$versionJavascript}.js"));
     $response = CM_Http_Response_Resource_Javascript_Library::createFromRequest($request, $site, $this->getServiceManager());
     $response->process();
     $this->assertNotContains('Hello World', $response->getContent());
     // Request JS-enabling of language key via RPC call
     $body = CM_Params::jsonEncode(['method' => 'CM_Model_Language.requestTranslationJs', 'params' => ['Hello World']]);
     $request = new CM_Http_Request_Post('/rpc', null, null, $body);
     $response = CM_Http_Response_RPC::createFromRequest($request, $site, $this->getServiceManager());
     $response->process();
     CMTest_TH::reinstantiateModel($languageKey);
     $this->assertSame(true, $languageKey->getJavascript());
     // Check that key *is* included in the JS translations list
     $versionJavascript = CM_Model_Language::getVersionJavascript();
     $request = new CM_Http_Request_Get($render->getUrlResource('library-js', "translations/{$versionJavascript}.js"));
     $response = CM_Http_Response_Resource_Javascript_Library::createFromRequest($request, $site, $this->getServiceManager());
     $response->process();
     $this->assertContains('Hello World', $response->getContent());
 }
Example #4
0
 /**
  * @return CM_Site_Abstract
  */
 public function getSite()
 {
     $site = $this->_site;
     if (null === $site) {
         $site = CM_Site_Abstract::factory();
     }
     return $site;
 }
Example #5
0
 public function testProcessingInvalidMethod()
 {
     $body = CM_Params::jsonEncode(['method' => 'foo']);
     $request = new CM_Http_Request_Post('/rpc/' . CM_Site_Abstract::factory()->getType(), null, null, $body);
     $response = new CM_Http_Response_RPC($request, $this->getServiceManager());
     $response->process();
     $responseData = CM_Params::jsonDecode($response->getContent());
     $this->assertSame(['error' => ['type' => 'CM_Exception_InvalidParam', 'msg' => 'Internal server error', 'isPublic' => false]], $responseData);
 }
Example #6
0
 public function testFactory()
 {
     try {
         CM_Site_Abstract::factory(9999);
         $this->fail('Factory returned non-configured site');
     } catch (CM_Class_Exception_TypeNotConfiguredException $ex) {
         $this->assertContains('Site with type `9999` not configured', $ex->getMessage());
     }
 }
Example #7
0
 public function testProcessHostRedirect()
 {
     $site = CM_Site_Abstract::factory();
     $response = CMTest_TH::createResponsePageEmbed('/mock7', array('host' => $site->getHost()));
     $response->process();
     $this->assertNull($response->getRedirectUrl());
     $response = CMTest_TH::createResponsePageEmbed('/mock7', array('host' => 'incorrect-host.org'));
     $response->process();
     $this->assertSame($site->getUrl() . '/mock7', $response->getRedirectUrl());
 }
Example #8
0
 public function testFactory()
 {
     try {
         CM_Site_Abstract::factory(9999);
         $this->fail('Factory returned non-configured site');
     } catch (CM_Class_Exception_TypeNotConfiguredException $ex) {
         $this->assertSame('Site with given type is not configured', $ex->getMessage());
         $this->assertSame(['siteType' => 9999], $ex->getMetaInfo());
     }
 }
Example #9
0
 public function testProcessHostRedirect_parameter()
 {
     $site = CM_Site_Abstract::factory();
     $location = CMTest_TH::createLocation();
     $locationEncoded = CM_Params::encode($location, true);
     $query = http_build_query(['location' => $locationEncoded]);
     $response = CMTest_TH::createResponsePage('/mock5?' . $query, array('host' => 'incorrect-host.org'));
     $response->process();
     $siteUrl = 'http://' . $site->getHost();
     $this->assertContains('Location: ' . $siteUrl . '/mock5?' . $query, $response->getHeaders());
 }
Example #10
0
 public function testSetNull()
 {
     $environment = new CM_Frontend_Environment();
     $this->assertEquals(CM_Site_Abstract::factory(), $environment->getSite());
     $this->assertNull($environment->getViewer());
     $this->assertNull($environment->getLanguage());
     $this->assertSame('en', $environment->getLocale());
     $this->assertEquals(CM_Bootloader::getInstance()->getTimeZone(), $environment->getTimeZone());
     $this->assertSame(CM_Bootloader::getInstance()->isDebug(), $environment->isDebug());
     $this->assertNull($environment->getLocation());
 }
Example #11
0
 public function testProcess()
 {
     $site = CM_Site_Abstract::factory();
     $render = new CM_Frontend_Render(new CM_Frontend_Environment());
     $request = new CM_Http_Request_Get($render->getUrlResource('library-css', 'all.css'));
     $response = CM_Http_Response_Resource_Css_Library::createFromRequest($request, $site, $this->getServiceManager());
     $response->process();
     $this->assertContains('Cache-Control: max-age=31536000', $response->getHeaders());
     $this->assertContains('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 31536000), $response->getHeaders());
     $this->assertContains('body{', $response->getContent());
 }
Example #12
0
 public function testLoadPageRedirectLanguage()
 {
     $site = CM_Site_Abstract::factory();
     CMTest_TH::createLanguage('en');
     $viewer = CMTest_TH::createUser();
     $environment = new CM_Frontend_Environment(null, $viewer);
     $component = new CM_Page_View_Ajax_Test_Mock();
     $response = $this->getResponseAjax($component, 'loadPage', ['path' => '/en' . CM_Page_View_Ajax_Test_Mock::getPath()], $environment);
     $this->assertViewResponseSuccess($response);
     $responseDecoded = CM_Params::jsonDecode($response->getContent());
     $this->assertSame($site->getUrl() . CM_Page_View_Ajax_Test_Mock::getPath(), $responseDecoded['success']['data']['url']);
 }
Example #13
0
 public function testSetSite()
 {
     $siteDefault = CM_Site_Abstract::factory();
     $user = CMTest_TH::createUser();
     $this->assertEquals($siteDefault, $user->getSite());
     $type = $siteDefault->getType() + 1;
     $site = $this->getMockBuilder('CM_Site_Abstract')->setMethods(array('getType'))->getMock();
     $site->expects($this->any())->method('getType')->will($this->returnValue($type));
     CM_Config::get()->CM_Site_Abstract->types[$type] = get_class($site);
     $user->setSite($site);
     $this->assertEquals($site, $user->getSite());
 }
Example #14
0
 public function testProcessMissingParameter()
 {
     $site = CM_Site_Abstract::factory();
     $request = new CM_Http_Request_Get('/emailtracking', ['host' => $site->getHost()]);
     $response = CM_Http_Response_EmailTracking::createFromRequest($request, $site, $this->getServiceManager());
     try {
         $response->process();
         $this->fail('Expected exception not thrown');
     } catch (CM_Exception_InvalidParam $e) {
         $this->assertSame(CM_Exception::WARN, $e->getSeverity());
     }
 }
Example #15
0
 public function testProcessAfterBody()
 {
     $site = CM_Site_Abstract::factory();
     $render = new CM_Frontend_Render(new CM_Frontend_Environment());
     $request = new CM_Http_Request_Get($render->getUrlResource('vendor-js', 'after-body.js'));
     $response = CM_Http_Response_Resource_Javascript_Vendor::createFromRequest($request, $site, $this->getServiceManager());
     $response->process();
     $this->assertContains('Cache-Control: max-age=31536000', $response->getHeaders());
     $this->assertContains('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 31536000), $response->getHeaders());
     $this->assertContains('function()', $response->getContent());
     $this->assertContains('jQuery', $response->getContent());
 }
Example #16
0
 public function testSetNull()
 {
     $defaultCurrency = CM_Model_Currency::create('840', 'USD');
     $environment = new CM_Frontend_Environment();
     $this->assertEquals(CM_Site_Abstract::factory(), $environment->getSite());
     $this->assertNull($environment->getViewer());
     $this->assertNull($environment->getLanguage());
     $this->assertSame('en', $environment->getLocale());
     $this->assertEquals(CM_Bootloader::getInstance()->getTimeZone(), $environment->getTimeZone());
     $this->assertSame(CM_Bootloader::getInstance()->isDebug(), $environment->isDebug());
     $this->assertNull($environment->getLocation());
     $this->assertEquals($defaultCurrency, $environment->getCurrency());
     $this->assertNull($environment->getClientDevice());
 }
Example #17
0
 public function testNonexistentAjaxMethodException()
 {
     /** @var CM_View_Abstract|PHPUnit_Framework_MockObject_MockObject $view */
     $view = $this->getMockForAbstractClass('CM_View_Abstract');
     $site = CM_Site_Abstract::factory();
     $request = $this->createRequestAjax($view, 'someReallyBadMethod', ['params' => 'foo']);
     $response = CM_Http_Response_View_Ajax::createFromRequest($request, $site, $this->getServiceManager());
     $exception = $this->catchException(function () use($response) {
         $response->process();
     });
     $this->assertInstanceOf('CM_Exception_Invalid', $exception);
     /** @var CM_Exception_Invalid $exception */
     $this->assertSame('Method not found', $exception->getMessage());
     $this->assertSame(['method' => 'ajax_someReallyBadMethod'], $exception->getMetaInfo());
 }
Example #18
0
 public function testProcessEmoticon()
 {
     $deployVersion = CM_App::getInstance()->getDeployVersion();
     $siteType = CM_Site_Abstract::factory()->getType();
     $usertext = new CM_Usertext_Usertext(new CM_Frontend_Render());
     $expectedValuePlain = "<img src=\"http://cdn.default.dev/layout/" . $siteType . "/" . $deployVersion . "/img/emoticon/cold_sweat.png\" class=\"emoticon emoticon-cold_sweat\" title=\":cold_sweat:\" />";
     $expectedValueMarkdown = "<p><img src=\"http://cdn.default.dev/layout/" . $siteType . "/" . $deployVersion . "/img/emoticon/cold_sweat.png\" class=\"emoticon emoticon-cold_sweat\" title=\":cold_sweat:\" /></p>";
     $usertext->setMode('escape');
     $this->assertSame('&lt;3', $usertext->transform('<3'));
     $usertext->setMode('oneline');
     $this->assertSame($expectedValuePlain, $usertext->transform(':-\\\\'));
     $usertext->setMode('simple');
     $this->assertSame($expectedValuePlain, $usertext->transform(':-\\\\'));
     $usertext->setMode('markdown');
     $this->assertSame($expectedValueMarkdown, $usertext->transform(':-\\\\'));
     $usertext->setMode('markdownPlain');
     $this->assertContains($expectedValuePlain, $usertext->transform(':-\\\\'));
 }
Example #19
0
 /**
  * @param CM_Page_Abstract      $page
  * @param CM_Model_User|null    $viewer
  * @param CM_Site_Abstract|null $site
  * @return CM_Dom_NodeList
  */
 protected function _renderPage(CM_Page_Abstract $page, CM_Model_User $viewer = null, CM_Site_Abstract $site = null)
 {
     if (null === $site) {
         $site = CM_Site_Abstract::factory();
     }
     $host = parse_url($site->getUrl(), PHP_URL_HOST);
     $request = new CM_Http_Request_Get('?' . http_build_query($page->getParams()->getParamsEncoded()), array('host' => $host), null, $viewer);
     $response = new CM_Http_Response_Page($request, $this->getServiceManager());
     $page->prepareResponse($response->getRender()->getEnvironment(), $response);
     $renderAdapter = new CM_RenderAdapter_Page($response->getRender(), $page);
     $html = $renderAdapter->fetch();
     return new CM_Dom_NodeList($html, true);
 }
Example #20
0
 public function testGetSiteDefault()
 {
     $mail = new CM_Mail();
     $this->assertEquals(CM_Site_Abstract::factory(), $mail->getSite());
 }
Example #21
0
 public function testUploadInvalidField()
 {
     $filename = 'test.jpg';
     $content = file_get_contents(DIR_TEST_DATA . 'img/' . $filename);
     $fileTmp = CM_File::create($this->_dir . 'test1', $content);
     $_FILES = array('file' => array('name' => $filename, 'tmp_name' => $fileTmp->getPath()));
     $site = CM_Site_Abstract::factory();
     $request = new CM_Http_Request_Post('/upload?field=nonexistent');
     $upload = CM_Http_Response_Upload::createFromRequest($request, $site, $this->getServiceManager());
     try {
         $upload->process();
         $this->fail('Should throw invalid exception');
     } catch (CM_Exception_Invalid $e) {
         $this->assertTrue(true);
     }
 }
Example #22
0
 /**
  * @return CM_Site_Abstract
  */
 public function popPathSiteByMatch()
 {
     $siteFactory = new CM_Site_SiteFactory();
     $site = $siteFactory->findSite($this);
     if (null === $site) {
         $site = CM_Site_Abstract::factory();
     }
     $sitePath = $site->getUrlParser()->getPath();
     if ($this->hasPathPrefix($sitePath)) {
         $this->popPathPrefix($sitePath);
     }
     return $site;
 }
Example #23
0
 /**
  * @param string                    $key
  * @param CM_Site_Abstract|int|null $default
  * @throws CM_Exception_InvalidParam
  * @return CM_Site_Abstract
  */
 public function getSite($key, $default = null)
 {
     $param = $this->_get($key, $default);
     if (ctype_digit($param) || is_int($param)) {
         return CM_Site_Abstract::factory($param);
     }
     if (!$param instanceof CM_Site_Abstract) {
         throw new CM_Exception_InvalidParam('Not a CM_Site_Abstract');
     }
     return $param;
 }
Example #24
0
 /**
  * @param CM_Frontend_Environment $environment
  * @param int                     $userInput
  * @return CM_Site_Abstract
  */
 public function validate(CM_Frontend_Environment $environment, $userInput)
 {
     $userInput = parent::validate($environment, $userInput);
     return CM_Site_Abstract::factory($userInput);
 }
Example #25
0
 public function testProcessExceptionCatching()
 {
     CM_Config::get()->CM_Http_Response_Page->exceptionsToCatch = ['CM_Exception_InvalidParam' => ['errorPage' => 'CM_Page_Error_NotFound', 'log' => false]];
     $this->getMockClass('CM_Layout_Abstract', null, [], 'CM_Layout_Default');
     $site = CM_Site_Abstract::factory();
     $request = new CM_Http_Request_Get('/example', ['host' => $site->getHost()]);
     /** @var CM_Http_Response_Page|\Mocka\AbstractClassTrait $response */
     $response = $this->mockObject('CM_Http_Response_Page', [$request, $site, $this->getServiceManager()]);
     $response->mockMethod('_renderPage')->set(function (CM_Page_Abstract $page) {
         if ($page instanceof CM_Page_Example) {
             throw new CM_Exception_InvalidParam();
         }
         return '<html>Error</html>';
     });
     $this->assertSame('/example', $response->getRequest()->getPath());
     $response->process();
     $this->assertSame('/error/not-found', $response->getRequest()->getPath());
 }
 public function testIsMail()
 {
     $emoticon = CM_Emoticon::findByCode(':-)');
     $siteDefault = CM_Site_Abstract::factory();
     $this->_assertSame('<span class="usertext oneline">foo <img src="http://cdn.default.dev/layout/' . $siteDefault->getId() . '/' . CM_App::getInstance()->getDeployVersion() . '/img/emoticon/smiley.png" class="emoticon emoticon-' . $emoticon->getName() . '" title=":smiley:" height="16" /></span>', array('text' => 'foo :-)', 'mode' => 'oneline', 'isMail' => true));
 }
Example #27
0
 public function testSetSite()
 {
     $siteDefault = CM_Site_Abstract::factory();
     $user = CMTest_TH::createUser();
     $this->assertEquals($siteDefault, $user->getSite());
     $site = $this->getMockSite();
     $user->setSite($site);
     $this->assertEquals($site, $user->getSite());
 }
Example #28
0
 /**
  * @return CM_Site_Abstract
  */
 public function getSite()
 {
     $siteType = $this->_get('site');
     return CM_Site_Abstract::factory($siteType);
 }
Example #29
0
File: TH.php Project: aladin1394/CM
 /**
  * @param string             $uri
  * @param array|null         $headers
  * @param CM_Model_User|null $viewer
  * @return CM_Http_Response_Page
  */
 public static function createResponsePageEmbed($uri, array $headers = null, CM_Model_User $viewer = null)
 {
     if (!$headers) {
         $site = CM_Site_Abstract::factory();
         $headers = array('host' => $site->getHost());
     }
     $request = new CM_Http_Request_Get($uri, $headers, null, $viewer);
     return new CM_Http_Response_Page_Embed($request, self::getServiceManager());
 }
Example #30
0
 public function testGetUrlResourceSameOrigin()
 {
     $render = new CM_Frontend_Render();
     $siteType = CM_Site_Abstract::factory()->getType();
     $deployVersion = CM_App::getInstance()->getDeployVersion();
     $this->assertSame('http://www.default.dev/layout/' . $siteType . '/' . $deployVersion . '/foo.jpg', $render->getUrlResource('layout', 'foo.jpg', ['sameOrigin' => true]));
 }