Exemplo n.º 1
0
 /**
  * @expectedException \Magelight\Exception
  * @expectedExceptionMessage Observer '\Magelight\Observer' method 'unexistentMethod' does not exist or is not callable!
  */
 public function testDispatchEventException()
 {
     $eventName = 'test_event';
     $observerClass1 = '\\Magelight\\Observer::unexistentMethod';
     $observers = [$observerClass1];
     $this->configMock->expects($this->once())->method('getConfigSet')->with('global/events/' . $eventName . '/observer')->will($this->returnValue($observers));
     $observerMock = $this->getMockForAbstractClass('\\Magelight\\Observer', [], '', false, false, true, ['execute']);
     \Magelight\Observer::forgeMock($observerMock);
     $this->eventManager->dispatchEvent('test_event');
 }
Exemplo n.º 2
0
 public function testGetAdapter()
 {
     $cacheConfig = new \SimpleXMLElement('<default>
             <type>file</type>
             <config>
                 <path>var/cache</path>
             </config>
         </default>
     ');
     $this->configMock->expects($this->once())->method('getConfig')->with('global/cache/default')->will($this->returnValue($cacheConfig));
     $fileAdapterMock = $this->getMock(\Magelight\Cache\Adapter\File::class, [], [], '', false);
     \Magelight\Cache\Adapter\File::forgeMock($fileAdapterMock);
     $this->assertEquals($fileAdapterMock, $this->adapterPool->getAdapter());
 }
Exemplo n.º 3
0
 public function testFlushAllCache()
 {
     $cacheConfig = new \SimpleXMLElement('<cache>
         <default>
             <type>file</type>
             <config>
                 <path>var/cache</path>
             </config>
         </default>
         <other>
             <memcache>
                 <server>
                     <host>127.0.0.1</host>
                     <port>11211</port>
                 </server>
             </memcache>
         </other>
     </cache>');
     $this->configMock->expects($this->once())->method('getConfig')->with('global/cache')->will($this->returnValue($cacheConfig));
     $adapterPoolMock = $this->getMock(\Magelight\Cache\AdapterPool::class, [], [], '', false);
     \Magelight\Cache\AdapterPool::forgeMock($adapterPoolMock);
     $adapterMock = $this->getMockForAbstractClass(\Magelight\Cache\AdapterAbstract::class, [], '', false, false, true, ['clear']);
     $adapterMock->expects($this->exactly(2))->method('clear');
     $adapterPoolMock->expects($this->at(0))->method('getAdapter')->with('default')->will($this->returnValue($adapterMock));
     $adapterPoolMock->expects($this->at(1))->method('getAdapter')->with('other')->will($this->returnValue($adapterMock));
     $this->app->flushAllCache();
 }
Exemplo n.º 4
0
 /**
  * Forgery constructor
  */
 public function __forge()
 {
     $this->setGetSetTargetArray($this->config);
     $this->config = array_merge($this->config, (array) \Magelight\Config::getInstance()->getConfig('global/document/captcha', []));
     $this->font_file = \Magelight\App::getInstance()->getRealPathInModules($this->font_file);
     $this->cleanup();
 }
Exemplo n.º 5
0
 /**
  * Get login form
  *
  * @return \Magelight\Webform\Blocks\Form
  */
 public function getLoginForm()
 {
     $form = Form::forge()->setHorizontal()->setConfigs('remindpass-form', $this->url(\Magelight\Config::getInstance()->getConfigString('global/auth/urls/login_url')));
     $fieldset = Fieldset::forge();
     $fieldset->addRowField(Elements\Input::forge()->setName('email'), __('E-Mail'));
     $fieldset->addRowField(Elements\PasswordInput::forge()->setName('password'), __('Password'));
     return $form->addFieldset($fieldset)->createResultRow(true)->addButtonsRow([Elements\Button::forge()->setContent(__('Enter'))->addClass('btn-primary'), Elements\Abstraction\Element::forge()->setTag('a')->setAttribute('href', $this->url('auth/remindpass'))->setContent(__('Remind password'))->setClass('btn')])->loadFromRequest(\Magelight\Http\Request::getInstance())->setValidator($this->getLoginFormValidator());
 }
Exemplo n.º 6
0
 /**
  * Test add log message
  */
 public function testAdd()
 {
     $configMock = $this->getMock(\Magelight\Config::class, [], [], '', false);
     \Magelight\Config::forgeMock($configMock);
     $configMock->expects($this->any())->method('getConfig')->with('global/log/file', \Magelight\Log::DEFAUL_LOG_FILE)->will($this->returnValue('application.log'));
     $this->log->expects($this->once())->method('writeMessage')->with($this->matchesRegularExpression("/[\\-\\:\\d]+\\s\\-\\serror/i"));
     $this->log->add('error');
 }
Exemplo n.º 7
0
 /**
  * Get for for password recovery page
  *
  * @return \Magelight\Webform\Blocks\Form
  */
 public function getForgotPasswordForm()
 {
     $form = Form::forge()->setHorizontal()->setConfigs('remindpass-form', $this->url(\Magelight\Config::getInstance()->getConfigString('global/auth/urls/forgot_password_url')));
     $fieldset = Fieldset::forge();
     $fieldset->addRowField(Elements\Input::forge()->setName('email'), __('E-Mail', [], 1, 'default'));
     $validator = \Magelight\Webform\Models\Validator::forge();
     $validator->fieldRules('email')->required()->setCustomError(__("Please enter a valid e-mail!", 1))->chainRule()->email()->setCustomError(__("Please enter a valid e-mail!", 1));
     return $form->addFieldset($fieldset)->createResultRow(true)->addButtonsRow([Elements\Button::forge()->setContent(__('Send new password'))->addClass('btn-primary')])->loadFromRequest()->setValidator($validator)->validateOnFront();
 }
Exemplo n.º 8
0
 public function testLoadFromCacheMiss()
 {
     $appConfig = '<config><global>
         <app>
             <cache_modules_config>1</cache_modules_config>
         </app>
     </global></config>';
     $expectedConfig = new \SimpleXMLElement('<config><global>
         <app>
             <cache_modules_config>0</cache_modules_config>
             <framework_config_data>framework</framework_config_data>
             <app_module_config_data>app.domain</app_module_config_data>
             <bool_data_true>1</bool_data_true>
             <bool_data_false>0</bool_data_false>
             <int_data>123</int_data>
             <float_data>123.456</float_data>
             <array_data>
                 <node_1>data</node_1>
                 <node_2>data_node_2</node_2>
             </array_data>
             <string_data>some string</string_data>
             <config_with_attribute attribute_1="attribute_value">config_string</config_with_attribute>
         </app>
     </global></config>');
     $this->appMock->expects($this->once())->method('getAppDir')->will($this->returnValue('/app'));
     $this->appMock->expects($this->once())->method('getModuleDirectories')->will($this->returnValue(['/app/modules', '/framework/modules']));
     $this->configLoaderMock->expects($this->at(0))->method('loadConfig')->with('/app' . DS . 'etc' . DS . 'config.xml');
     $this->configLoaderMock->expects($this->at(1))->method('getConfig')->will($this->returnValue(new \SimpleXMLElement($appConfig)));
     $this->modulesMock->expects($this->any())->method('getActiveModules')->will($this->returnValue([['name' => 'Magelight_Module', 'path' => 'Magelight\\Module'], ['name' => 'App_Module', 'path' => 'App\\Module']]));
     $this->configLoaderMock->expects($this->any())->method('getModulesConfigFilePath')->will($this->returnValueMap([['/framework/modules', ['name' => 'Magelight_Module', 'path' => 'Magelight\\Module'], '/framework/modules/Magelight\\Module' . DS . 'etc' . DS . 'config.xml'], ['/app/modules', ['name' => 'Magelight_Module', 'path' => 'Magelight\\Module'], null], ['/framework/modules', ['name' => 'App_Module', 'path' => 'App\\Module'], null], ['/app/modules', ['name' => 'App_Module', 'path' => 'App\\Module'], '/app/modules/App\\Module' . DS . 'etc' . DS . 'config.xml']]));
     $this->configLoaderMock->expects($this->at(3))->method('loadConfig')->with('/framework/modules/Magelight\\Module' . DS . 'etc' . DS . 'config.xml');
     $this->configLoaderMock->expects($this->at(7))->method('loadConfig')->with('/app/modules/App\\Module' . DS . 'etc' . DS . 'config.xml');
     $this->configLoaderMock->expects($this->at(8))->method('getConfig')->will($this->returnValue($expectedConfig));
     $this->cacheAdapterMock->expects($this->once())->method('set')->with($this->config->buildCacheKey('modules_config'), $expectedConfig->asXML(), 3600);
     $this->config->load();
     $this->assertEquals('framework', $this->config->getConfigString('/global/app/framework_config_data'));
     $this->assertEquals('app.domain', $this->config->getConfigString('/global/app/app_module_config_data'));
     $this->assertTrue($this->config->getConfigBool('/global/app/bool_data_true'));
     $this->assertFalse($this->config->getConfigBool('/global/app/bool_data_false'));
     $this->assertInternalType('int', $this->config->getConfigInt('/global/app/int_data'));
     $this->assertEquals(123, $this->config->getConfigInt('/global/app/int_data'));
     $this->assertInternalType('float', $this->config->getConfigFloat('/global/app/float_data'));
     $this->assertEquals(123.456, $this->config->getConfigFloat('/global/app/float_data'));
     $this->assertInternalType('array', $this->config->getConfigArray('/global/app/array_data'));
     $this->assertEquals(['node_1' => 'data', 'node_2' => 'data_node_2'], $this->config->getConfigArray('/global/app/array_data'));
     $this->assertInternalType('string', $this->config->getConfigString('/global/app/string_data'));
     $this->assertEquals('some string', $this->config->getConfigString('/global/app/string_data'));
     $this->assertInternalType('string', $this->config->getConfigAttribute('/global/app/config_with_attribute', 'attribute_1'));
     $this->assertEquals('attribute_value', $this->config->getConfigAttribute('/global/app/config_with_attribute', 'attribute_1'));
     $this->assertInternalType('string', $this->config->getConfigString('/global/app/unexistent_node', 'default_value'));
     $this->assertEquals('default_value', $this->config->getConfigString('/global/app/unexistent_node', 'default_value'));
     $this->assertInternalType('array', $this->config->getConfigSet('/global/app'));
     $this->assertInternalType('string', $this->config->getConfigXmlString());
     $this->assertEquals($expectedConfig->asXML(), $this->config->getConfigXmlString());
 }
Exemplo n.º 9
0
 /**
  * Get sample form
  *
  * @return Form
  */
 public function getSampleForm()
 {
     $form = Form::forge()->setHorizontal()->setConfigs('sample', $this->url('sample/form'));
     $fieldset = Fieldset::forge();
     $fieldset->setLegend('Register new user');
     $fieldset->addRowField(Elements\Input::forge()->setName('name'), __('Name'));
     $fieldset->addRowField(Elements\Input::forge()->setName('email'), __('E-Mail'));
     $fieldset->addRowField(Elements\PasswordInput::forge()->setName('password'), __('Password'));
     $fieldset->addRowField(Elements\Captcha::forge($this->url(\Magelight\Config::getInstance()->getConfigString('global/auth/urls/render_captcha_url')))->setName('captcha')->addClass('col-md-6'), __('Enter protection code'));
     return $form->addFieldset($fieldset)->createResultRow(true)->addButtonsRow(Elements\Button::forge()->setContent(__('Register'))->addClass('btn-primary'))->loadFromRequest()->setValidator($this->getSampleFormValidator())->validateOnFront();
 }
Exemplo n.º 10
0
 public function setUp()
 {
     $this->modulesMock = $this->getMock(\Magelight\Components\Modules::class, [], [], '', false);
     $this->configMock = $this->getMock(\Magelight\Config::class, [], [], '', false);
     $this->dbMock = $this->getMock(\Magelight\Db\Mysql\Adapter::class, [], [], '', false);
     $this->appMock = $this->getMockForAbstractClass(App::class, [], '', false, false, true, ['db']);
     \Magelight\Components\Modules::forgeMock($this->modulesMock);
     \Magelight\Config::forgeMock($this->configMock);
     \Magelight\Db\Mysql\Adapter::forgeMock($this->dbMock);
     \Magelight\App::forgeMock($this->appMock);
 }
Exemplo n.º 11
0
 /**
  * Get url by match
  * 
  * @param string $match
  * @param array  $params
  * @param string $type
  * @param bool $addOnlyMaskParams - add to url only params that are present in URL match mask
  *
  * @return string
  * @throws \Magelight\Exception
  */
 public function getUrl($match, $params = [], $type = null, $addOnlyMaskParams = false)
 {
     if (!$type) {
         $type = \Magelight\Config::getInstance()->getConfigString('global/app/default_url_type', self::TYPE_HTTP);
     }
     $match = '/' . trim($match, '\\/');
     if (!$addOnlyMaskParams) {
         if (\Magelight\App::getInstance()->isInDeveloperMode() && !$this->checkParamsWithPlaceholderMask($match, $params)) {
             throw new \Magelight\Exception("Passed url params don`t match mask that is set for parameter or default mask: " . \Magelight\Components\Loaders\Routes::DEFAULT_REGEX . ". Change the mask for parameter or use concatenation for complex URLs", E_USER_NOTICE);
         }
     }
     $params = $this->flatternParams($params);
     return $this->getBaseUrl($type) . $this->makeRequestUri($match, $params, $addOnlyMaskParams);
 }
Exemplo n.º 12
0
 /**
  * Forgery constructor
  *
  * @param \SimpleXMLElement $config
  * @throws \Magelight\Exception
  */
 public function __forge($config)
 {
     $this->config = $config;
     if (isset($config->cache_key_prefix)) {
         $this->cacheKeyPrefix = (string) $config->cache_key_prefix;
     } else {
         $this->cacheKeyPrefix = (string) \Magelight\Config::getInstance()->getConfig('global/base_domain');
         if (!$this->cacheKeyPrefix) {
             $this->cacheKeyPrefix = md5(\Magelight\App::getInstance()->getAppDir());
             if (!$this->cacheKeyPrefix) {
                 throw new \Magelight\Exception('Cache key prefix not set, and base domain too. Cache conflicts can appear.');
             }
         }
     }
     $this->init();
 }
Exemplo n.º 13
0
 /**
  * Dispatch application event (executes all observers that were bound to this event)
  *
  * @param string $eventName
  * @param array $arguments
  * @throws \Magelight\Exception
  */
 public function dispatchEvent($eventName, $arguments = [])
 {
     $observers = (array) \Magelight\Config::getInstance()->getConfigSet('global/events/' . $eventName . '/observer');
     if (!empty($observers)) {
         foreach ($observers as $observerClass) {
             $observerClass = explode('::', (string) $observerClass);
             $class = $observerClass[0];
             $method = isset($observerClass[1]) ? $observerClass[1] : 'execute';
             $observer = call_user_func_array([$class, 'forge'], [$arguments]);
             /* @var $observer \Magelight\Observer*/
             if (!method_exists($observer, $method)) {
                 throw new \Magelight\Exception("Observer '{$class}' method '{$method}' does not exist or is not callable!");
             }
             $observer->{$method}();
         }
     }
 }
Exemplo n.º 14
0
 /**
  * Run app
  *
  * @throws \Exception|Exception
  */
 public function run()
 {
     try {
         \Magelight\Event\Manager::getInstance()->dispatchEvent('app_start', []);
         $request = \Magelight\Http\Request::getInstance();
         $resource = $request->getGet('resource');
         $staticDir = realpath($this->getAppDir() . DS . \Magelight\Config::getInstance()->getConfigString('global/view/published_static_dir', 'pub/static'));
         foreach (array_reverse($this->getModuleDirectories()) as $modulesPath) {
             $resource = str_replace('\\/', DS, $resource);
             $filename = $modulesPath . DS . $resource;
             $targetFilename = $staticDir . DS . $resource;
             if (file_exists($filename)) {
                 if (!is_dir(dirname($targetFilename))) {
                     mkdir(dirname($targetFilename), 0777, true);
                 }
                 if (\Magelight\Config::getInstance()->getConfigBool('global/app/developer_mode', false)) {
                     $pathinfo = pathinfo($filename, PATHINFO_EXTENSION);
                     if (isset($this->mimeTypes[$pathinfo])) {
                         $mimeType = $this->mimeTypes[$pathinfo];
                         header('Content-type: ' . $mimeType);
                     }
                     echo file_get_contents($filename);
                     break;
                 }
                 copy($filename, $targetFilename);
                 header('Location: ' . \Magelight\Helpers\UrlHelper::getInstance()->getUrl($resource));
                 break;
             }
         }
     } catch (\Exception $e) {
         \Magelight\Log::getInstance()->add($e->getMessage());
         if ($this->developerMode) {
             throw $e;
         }
     }
 }
Exemplo n.º 15
0
 public function testDateTime()
 {
     $configMock = $this->getMock(\Magelight\Config::class, [], [], '', false);
     $configMock->expects($this->once())->method('getConfig')->with('global/view/date_time_format', 'Y-m-d H:i:s')->will($this->returnValue('Y-m-d H:i:s'));
     \Magelight\Config::forgeMock($configMock);
     $date = time();
     $expectedDateString = date('Y-m-d H:i:s', $date);
     $this->assertEquals($expectedDateString, \Magelight\Block::forge()->dateTime($date));
 }
Exemplo n.º 16
0
 /**
  * Minify document static entry
  *
  * @param string $type
  * @param array $staticEntries
  * @return array
  * @throws \Magelight\Exception
  */
 protected function minifyDocumentStatic($type = 'css', $staticEntries = [])
 {
     $minifier = $this->getMinifierByType($type);
     $content = '';
     $path = $this->getEntriesStaticPath($staticEntries, $type);
     $dir = dirname($path);
     if (!is_writable($dir)) {
         trigger_error(__("Static cache directory %s is not writable or does not exist!", [$dir]));
         return $staticEntries;
     }
     if ($isAlreadyMinified = $this->cache()->get($this->buildCacheKey($path), false)) {
         $ok = true;
         if (\Magelight\Config::getInstance()->getConfigBool('global/minifier/check_readability')) {
             $ok = is_readable($path);
         }
         if ($ok) {
             return ['path' => $path, 'content' => '', 'url' => \Magelight\Helpers\UrlHelper::getInstance()->getUrl($path), 'inline' => false];
         }
     }
     foreach ($staticEntries as $entry) {
         if ($entry['inline']) {
             $content .= $minifier->minify($entry['content']);
         } else {
             $buffer = file_get_contents(\Magelight\App::getInstance()->getRealPathInModules($entry['path']));
             if ($buffer === false) {
                 trigger_error(__("File %s for minifier cannot be read", [$entry['path']]), E_USER_WARNING);
             }
             if (\Magelight\Config::getInstance()->getConfigBool('global/minifier/compress_' . $type)) {
                 $buffer = $minifier->minify($buffer);
             }
             switch ($type) {
                 case 'css':
                     $content .= $this->fixCssUrls($buffer, $entry['path']);
                     break;
                 case 'js':
                     $content .= $buffer;
                     break;
                 default:
                     break;
             }
             unset($buffer);
         }
     }
     if (file_put_contents($path, $content)) {
         $this->cache()->set($this->buildCacheKey($path), 1, \Magelight\Config::getInstance()->getConfigInt('global/minifier/cache_ttl_' . $type));
         return ['path' => $path, 'content' => '', 'url' => \Magelight\Helpers\UrlHelper::getInstance()->getUrl($path), 'inline' => false];
     } else {
         return $staticEntries;
     }
 }
Exemplo n.º 17
0
 /**
  * Get adapter by index
  *
  * @param string $index
  * @return \Magelight\Cache\AdapterAbstract
  */
 public function getAdapter($index = \Magelight\App::DEFAULT_INDEX)
 {
     $config = \Magelight\Config::getInstance()->getConfig('global/cache/' . $index);
     $type = $this->getAdapterClassByType((bool) $config->disabled ? 'dummy' : $config->type);
     return call_user_func_array([$type, 'getInstance'], [$config->config]);
 }
Exemplo n.º 18
0
 /**
  * Get entities configuration
  *
  * @return mixed|\SimpleXMLElement
  */
 public function getEntitiesConfig()
 {
     if (empty($this->entitiesConfig)) {
         $entitiesConfig = clone \Magelight\Config::getInstance()->getConfig('admin/scaffold/entities');
         $this->defaultEntityConfig = clone $entitiesConfig->default;
         unset($entitiesConfig->default);
         foreach ($entitiesConfig->children() as $child) {
             /** @var $child \SimpleXMLElement */
             /** @var $draft \SimpleXMLElement */
             /** @var $fieldDraft \SimpleXMLElement */
             $draft = clone $this->defaultEntityConfig;
             \Magelight\Components\Loaders\Config::mergeConfig($draft, $child);
             \Magelight\Components\Loaders\Config::mergeConfig($entitiesConfig->{$child->getName()}, $draft);
             $defaultFieldConfig = clone $entitiesConfig->{$child->getName()}->fields->default;
             unset($entitiesConfig->{$child->getName()}->fields->default);
             foreach ($entitiesConfig->{$child->getName()}->fields->children() as $field) {
                 $fieldDraft = clone $defaultFieldConfig;
                 \Magelight\Components\Loaders\Config::mergeConfig($fieldDraft, $field);
                 \Magelight\Components\Loaders\Config::mergeConfig($entitiesConfig->{$child->getName()}->fields->{$field->getName()}, $fieldDraft);
             }
         }
         $this->entitiesConfig = $entitiesConfig;
     }
     return $this->entitiesConfig;
 }
Exemplo n.º 19
0
 /**
  * Logout action handler
  */
 public function logoutAction()
 {
     $this->session()->unsetData('user_id');
     $this->redirect($this->url(\Magelight\Config::getInstance()->getConfig('global/auth/urls/success_url')));
 }
Exemplo n.º 20
0
 /**
  * Flush all application caches
  *
  * @return App
  */
 public function flushAllCache()
 {
     $config = \Magelight\Config::getInstance()->getConfig('global/cache');
     foreach ($config->children() as $index => $cache) {
         $adapters[] = \Magelight\Cache\AdapterPool::getInstance()->getAdapter($index)->clear();
     }
     return $this;
 }
Exemplo n.º 21
0
 /**
  * Load block layout from config node
  *
  * @param string $layoutIndex - path to perspective in config
  * @return Block
  */
 public function loadLayout($layoutIndex = 'global/layouts/default')
 {
     return $this->processLayout(\Magelight\Config::getInstance()->getConfig($layoutIndex));
 }
Exemplo n.º 22
0
 /**
  * Initialize application logger
  */
 protected function init()
 {
     $this->file = (string) \Magelight\Config::getInstance()->getConfig('global/log/file', self::DEFAUL_LOG_FILE);
     $this->initialized = true;
     return $this;
 }
Exemplo n.º 23
0
 /**
  * Before To HTML
  *
  * @return \Magelight\Block|void
  */
 public function beforeToHtml()
 {
     $lang = \Magelight\Config::getInstance()->getConfigString('global/document/default_lang');
     $this->setLang($lang);
     $this->head->addContent($this->renderTitle());
     $this->head->addContent($this->renderMeta());
     $this->head->addContent($this->renderCss());
     $this->head->addContent($this->renderJs());
     $this->body->addContent($this->section('body'));
     return $this;
 }
Exemplo n.º 24
0
 /**
  * Load items
  */
 protected function loadItems()
 {
     $items = \Magelight\Config::getInstance()->getConfig('admin/navbar/items');
     $this->items = $this->getItemForgery()->forgeItems($items);
 }
Exemplo n.º 25
0
 /**
  * Get Ulogin JS url
  *
  * @return string
  */
 public function getUloginScriptUrl()
 {
     return (string) \Magelight\Config::getInstance()->getConfig('global/auth/ulogin/ulogin_script_url');
 }
Exemplo n.º 26
0
 /**
  * Forgery constructor
  */
 public function __forge()
 {
     $this->setPrivateKey((string) \Magelight\Config::getInstance()->getConfigString('global/document/re_captcha/private_key'));
     $this->setPublicKey((string) \Magelight\Config::getInstance()->getConfigString('global/document/re_captcha/public_key'));
 }