Beispiel #1
0
 public function iconRefresh()
 {
     /** @var CM_File[] $svgFileList */
     $svgFileList = array();
     foreach (CM_Bootloader::getInstance()->getModules() as $moduleName) {
         $iconPath = CM_Util::getModulePath($moduleName) . 'layout/default/resource/img/icon/';
         foreach (glob($iconPath . '*.svg') as $svgPath) {
             $svgFile = new CM_File($svgPath);
             $svgFileList[strtolower($svgFile->getFileName())] = $svgFile;
         }
     }
     if (0 === count($svgFileList)) {
         throw new CM_Exception_Invalid('Cannot process `0` icons');
     }
     $this->_getStreamOutput()->writeln('Processing ' . count($svgFileList) . ' unique icons...');
     $dirWork = CM_File::createTmpDir();
     $dirBuild = $dirWork->joinPath('/build');
     foreach ($svgFileList as $fontFile) {
         $fontFile->copyToFile($dirWork->joinPath($fontFile->getFileName()));
     }
     CM_Util::exec('fontcustom', array('compile', $dirWork->getPathOnLocalFilesystem(), '--no-hash', '--autowidth', '--font-name=icon-webfont', '--output=' . $dirBuild->getPathOnLocalFilesystem()));
     $cssFile = $dirBuild->joinPath('/icon-webfont.css');
     $less = preg_replace('/url\\("(?:.*?\\/)(.+?)(\\??#.+?)?"\\)/', 'url(urlFont("\\1") + "\\2")', $cssFile->read());
     CM_File::create(DIR_PUBLIC . 'static/css/library/icon.less', $less);
     foreach (glob($dirBuild->joinPath('/icon-webfont.*')->getPathOnLocalFilesystem()) as $fontPath) {
         $fontFile = new CM_File($fontPath);
         $fontFile->rename(DIR_PUBLIC . 'static/font/' . $fontFile->getFileName());
     }
     $dirWork->delete(true);
     $this->_getStreamOutput()->writeln('Created web-font and stylesheet.');
 }
Beispiel #2
0
 /**
  * @param string $namespace
  */
 private function _dbToFileSql($namespace)
 {
     $namespace = (string) $namespace;
     $tables = CM_Db_Db::exec("SHOW TABLES LIKE ?", array(strtolower($namespace) . '_%'))->fetchAllColumn();
     sort($tables);
     $dump = CM_Db_Db::getDump($tables, true);
     CM_File::create(CM_Util::getModulePath($namespace) . '/resources/db/structure.sql', $dump);
 }
Beispiel #3
0
 public function testGetClasses()
 {
     $classPaths = array('CM_Class_Abstract' => 'CM/Class/Abstract.php', 'CM_Paging_Abstract' => 'CM/Paging/Abstract.php', 'CM_Paging_Action_Abstract' => 'CM/Paging/Action/Abstract.php', 'CM_Paging_Action_User' => 'CM/Paging/Action/User.php');
     foreach ($classPaths as $className => &$path) {
         $path = CM_Util::getModulePath(CM_Util::getNamespace($className)) . 'library/' . $path;
     }
     $paths = array_reverse($classPaths);
     $this->assertSame(array_flip($classPaths), CM_Util::getClasses($paths));
 }
Beispiel #4
0
 /**
  * @param int $id
  * @throws CM_Exception_Nonexistent
  */
 public function __construct($id)
 {
     $this->_id = (int) $id;
     $this->_text = CM_Db_Db::select('cm_captcha', 'number', array('captcha_id' => $this->getId()))->fetchColumn();
     if (!$this->_text) {
         throw new CM_Exception_Nonexistent('Invalid captcha id `' . $id . '`', CM_Exception::WARN);
     }
     $this->_fontPath = CM_Util::getModulePath('CM') . 'resources/font/comicsans.ttf';
 }
Beispiel #5
0
 /**
  * @return array
  */
 private function _getColorStyles()
 {
     $site = $this->getParams()->getSite('site');
     $style = '';
     foreach (array_reverse($site->getModules()) as $moduleName) {
         $file = new CM_File(CM_Util::getModulePath($moduleName) . 'layout/default/variables.less');
         if ($file->exists()) {
             $style .= $file->read() . PHP_EOL;
         }
     }
     preg_match_all('#@(color\\w+)#', $style, $matches);
     $colors = array_unique($matches[1]);
     foreach ($colors as $variableName) {
         $style .= '.' . $variableName . ' { background-color: @' . $variableName . '; }' . PHP_EOL;
     }
     $lessCompiler = new lessc();
     $style = $lessCompiler->compile($style);
     preg_match_all('#.(color\\w+)\\s+\\{([^}]+)\\}#', $style, $matches);
     return array_combine($matches[1], $matches[2]);
 }
<?php

require_once CM_Util::getModulePath('CM') . 'library/CM/SmartyPlugins/function.checkbox.php';
class smarty_function_checkboxTest extends CMTest_TestCase
{
    /** @var Smarty_Internal_Template */
    private $_template;
    public function setUp()
    {
        $smarty = new Smarty();
        $render = new CM_Frontend_Render();
        $this->_template = $smarty->createTemplate('string:');
        $this->_template->assignGlobal('render', $render);
    }
    public function testDefault()
    {
        $this->_assertSame('<input type="checkbox" id="foo"><label for="foo"><span class="label">Hello</span></label>', array('id' => 'foo', 'label' => 'Hello'));
    }
    /**
     * @expectedException ErrorException
     */
    public function testNoLabel()
    {
        smarty_function_checkbox(['id' => 'foo'], $this->_template);
    }
    public function testNoId()
    {
        $html = smarty_function_checkbox(['label' => 'Hello'], $this->_template);
        $this->assertRegExp('#<input type="checkbox" id=".+">#', $html);
    }
    public function testChecked()
Beispiel #7
0
 /**
  * @param int         $version
  * @param string|null $moduleName
  * @return string
  * @throws CM_Exception_Invalid
  */
 public function _getUpdateScriptPath($version, $moduleName = null)
 {
     $path = DIR_ROOT;
     if ($moduleName) {
         $path = CM_Util::getModulePath($moduleName);
     }
     $file = new CM_File($path . 'resources/db/update/' . $version . '.php');
     if (!$file->exists()) {
         throw new CM_Exception_Invalid('Update script `' . $version . '` does not exist for `' . $moduleName . '` namespace.');
     }
     return $file->getPath();
 }
<?php

require_once CM_Util::getModulePath('CM') . 'library/CM/SmartyPlugins/function.button_link.php';
class smarty_function_button_linkTest extends CMTest_TestCase
{
    /** @var CM_Frontend_Render */
    private $_render;
    /** @var Smarty_Internal_Template */
    private $_template;
    public function setUp()
    {
        $smarty = new Smarty();
        $this->_render = new CM_Frontend_Render();
        $this->_template = $smarty->createTemplate('string:');
        $this->_template->assignGlobal('render', $this->_render);
    }
    public function testRender()
    {
        $params = array('action' => 'Create', 'label' => 'Some text <br /> with html tags', 'theme' => 'highlight', 'class' => 'button-large');
        $this->_assertContains('<span class="label">Some text <br /> with html tags</span>', array_merge($params, array('isHtmlLabel' => true)));
        $this->_assertContains('<span class="label">Some text &lt;br /&gt; with html tags</span>', array_merge($params, array('isHtmlLabel' => false)));
    }
    public function testRenderHref()
    {
        $this->assertRegExp('#<a.*href="http://example.com".*>.*Click me.*</a>#', smarty_function_button_link(['label' => 'Click me', 'href' => 'http://example.com'], $this->_template));
    }
    public function testRenderPage()
    {
        $urlPage = $this->_render->getUrlPage('CM_Page_Example', ['foo' => '12']);
        $this->assertRegExp('#<a.*href="' . preg_quote($urlPage) . '".*>.*Click me.*</a>#', smarty_function_button_link(['label' => 'Click me', 'page' => 'CM_Page_Example', 'foo' => '12'], $this->_template));
    }
<?php

require_once CM_Util::getModulePath('CM') . 'library/CM/SmartyPlugins/function.lessVariable.php';
class smarty_function_lessVariableTest extends CMTest_TestCase
{
    /** @var  Smarty_Internal_Template */
    private $_template;
    public function setUp()
    {
        $smarty = new Smarty();
        $render = new CM_Frontend_Render();
        $this->_template = $smarty->createTemplate('string:');
        $this->_template->assignGlobal('render', $render);
    }
    public function testColor()
    {
        $this->assertSame('#2d78e2', smarty_function_lessVariable(['name' => 'colorBrand'], $this->_template));
    }
    public function testSize()
    {
        $this->assertSame('14px', smarty_function_lessVariable(['name' => 'fontSize'], $this->_template));
    }
    /**
     * @expectedException Exception
     * @expectedExceptionMessage is undefined
     */
    public function testInvalidName()
    {
        smarty_function_lessVariable(['name' => 'helloworld'], $this->_template);
    }
}
<?php

require_once CM_Util::getModulePath('CM') . 'library/CM/SmartyPlugins/prefilter.translate.php';
class smarty_function_translateVariableTest extends CMTest_TestCase
{
    public function testTranslatePhrase()
    {
        /** @var CM_Frontend_Render|\Mocka\AbstractClassTrait $render */
        $render = $this->mockClass('CM_Frontend_Render')->newInstance();
        $getTranslationMethod = $render->mockMethod('getTranslation')->set(function ($key, $params) {
            $this->assertSame('Bar value is {$bar}', $key);
            $this->assertSame(['bar' => 3], $params);
        });
        /** @var CM_Frontend_Render $render */
        $object = new CM_I18n_Phrase('Bar value is {$bar}', ['bar' => 3]);
        $render->parseTemplateContent('{translateVariable key=$foo}', ['foo' => $object]);
        $this->assertSame(1, $getTranslationMethod->getCallCount());
        $exception = $this->catchException(function () use($render, $object) {
            $render->parseTemplateContent('{translateVariable key=$foo more=one}', ['foo' => $object]);
        });
        $this->assertInstanceOf('InvalidArgumentException', $exception);
        $this->assertSame('Passed params will be ignored as you provided CM_I18n_Phrase object', $exception->getMessage());
    }
}
<?php

require_once CM_Util::getModulePath('CM') . 'library/CM/SmartyPlugins/function.date_period.php';
function smarty_function_numberDecimal(array $params, Smarty_Internal_Template $template)
{
    $value = $params['value'];
    if (!is_numeric($value)) {
        throw new CM_Exception_Invalid('Invalid non-numeric value');
    }
    /** @var CM_Frontend_Render $render */
    $render = $template->getTemplateVars('render');
    $formatter = new NumberFormatter($render->getLocale(), NumberFormatter::DECIMAL);
    return $formatter->format($value);
}
<?php

require_once CM_Util::getModulePath('CM') . 'library/CM/SmartyPlugins/function.usertext.php';
class smarty_function_usertextTest extends CMTest_TestCase
{
    /** @var Smarty_Internal_Template */
    private $_template;
    public function setUp()
    {
        $smarty = new Smarty();
        $render = new CM_Frontend_Render();
        $this->_template = $smarty->createTemplate('string:');
        $this->_template->assignGlobal('render', $render);
    }
    public function tearDown()
    {
        CMTest_TH::clearEnv();
    }
    public function testModeEscape()
    {
        $this->_assertSame('foo&lt;', array('text' => 'foo<', 'mode' => 'escape'));
    }
    public function testModeOneline()
    {
        $this->_assertSame('<span class="usertext oneline">foo</span>', array('text' => 'foo', 'mode' => 'oneline'));
    }
    public function testModeSimple()
    {
        $this->_assertSame("<span class=\"usertext simple\">foo<br />\nbar</span>", array('text' => "foo  \nbar   \n", 'mode' => 'simple'));
    }
    public function testModeMarkdown()
Beispiel #13
0
 /**
  * @param string       $className
  * @param boolean|null $includeAbstracts
  * @return string[]
  */
 public static function getClassChildren($className, $includeAbstracts = null)
 {
     $key = CM_CacheConst::ClassChildren . '_className:' . $className . '_abstracts:' . (int) $includeAbstracts;
     $cache = CM_Cache_Local::getInstance();
     if (false === ($classNames = $cache->get($key))) {
         $pathsFiltered = array();
         $paths = array();
         foreach (CM_Bootloader::getInstance()->getModules() as $modulePath) {
             $namespacePaths = CM_Util::rglob('*.php', CM_Util::getModulePath($modulePath) . 'library/');
             $paths = array_merge($paths, $namespacePaths);
         }
         $regexp = '#\\bclass\\s+(?<name>[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\\s+#';
         foreach ($paths as $path) {
             $file = new CM_File($path);
             $fileContents = $file->read();
             if (preg_match($regexp, $fileContents, $matches)) {
                 if (class_exists($matches['name'], true)) {
                     $reflectionClass = new ReflectionClass($matches['name']);
                     if (($reflectionClass->isSubclassOf($className) || interface_exists($className) && $reflectionClass->implementsInterface($className)) && (!$reflectionClass->isAbstract() || $includeAbstracts)) {
                         $pathsFiltered[] = $path;
                     }
                 }
             }
         }
         $classNames = self::getClasses($pathsFiltered);
         $cache->set($key, $classNames);
     }
     return $classNames;
 }
Beispiel #14
0
<?php

require_once CM_Util::getModulePath('CM') . 'library/CM/SmartyPlugins/function.gravatar.php';
class smarty_function_gravatarTest extends CMTest_TestCase
{
    /**
     * @var Smarty_Internal_Template
     */
    private $_template;
    public function setUp()
    {
        $smarty = new Smarty();
        $render = new CM_Frontend_Render();
        $this->_template = $smarty->createTemplate('string:');
        $this->_template->assignGlobal('render', $render);
    }
    public function testRender()
    {
        $this->_assertSame('<img src="https://secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0" />', array('email' => '*****@*****.**'));
        $this->_assertSame('<img src="https://secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?s=140&amp;d=http%3A%2F%2Fexample.com%2Fdefault.jpg" />', array('email' => '*****@*****.**', 'size' => 140, 'default' => 'http://example.com/default.jpg'));
        $this->_assertSame('<img src="https://secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?s=140&amp;d=http%3A%2F%2Fexample.com%2Fdefault.jpg" class="TestClass" title="TestTitle" alt="TestTitle" width="20" height="20" />', array('email' => '*****@*****.**', 'size' => 140, 'default' => 'http://example.com/default.jpg', 'class' => 'TestClass', 'title' => 'TestTitle', 'width' => 20, 'height' => 20));
    }
    /**
     * @param string $expected
     * @param array  $params
     */
    private function _assertSame($expected, array $params)
    {
        $this->assertSame($expected, smarty_function_gravatar($params, $this->_template));
    }
}
<?php

require_once CM_Util::getModulePath('CM') . 'library/CM/SmartyPlugins/block.contentPlaceholder.php';
class smarty_block_contentPlaceholderTest extends CMTest_TestCase
{
    /**
     * @var Smarty_Internal_Template
     */
    private $_template;
    public function setUp()
    {
        $smarty = new Smarty();
        $render = new CM_Frontend_Render();
        $this->_template = $smarty->createTemplate('string:');
        $this->_template->assignGlobal('render', $render);
    }
    public function testRenderRatio()
    {
        $params = ['height' => 600, 'width' => 900];
        $size = $this->_getImgSize($params);
        $this->assertEquals(900, $size['width']);
        $this->assertEquals(600, $size['height']);
    }
    /**
     * @param array $params
     * @return String
     */
    private function _getImgSize(array $params)
    {
        $output = smarty_block_contentPlaceholder($params, '', $this->_template, false);
        $matches = array();
Beispiel #16
0
 /**
  * @return Smarty
  */
 private function _getSmarty()
 {
     if (!isset(self::$_smarty)) {
         self::$_smarty = new Smarty();
         self::$_smarty->setTemplateDir(DIR_ROOT);
         self::$_smarty->setCompileDir(CM_Bootloader::getInstance()->getDirTmp() . 'smarty/');
         self::$_smarty->_file_perms = 0666;
         self::$_smarty->_dir_perms = 0777;
         self::$_smarty->compile_check = CM_Bootloader::getInstance()->isDebug();
         self::$_smarty->caching = false;
         self::$_smarty->error_reporting = error_reporting();
     }
     $pluginDirs = array(SMARTY_PLUGINS_DIR);
     foreach ($this->getSite()->getModules() as $moduleName) {
         $pluginDirs[] = CM_Util::getModulePath($moduleName) . 'library/' . $moduleName . '/SmartyPlugins';
     }
     self::$_smarty->setPluginsDir($pluginDirs);
     self::$_smarty->loadFilter('pre', 'translate');
     return self::$_smarty;
 }
Beispiel #17
0
 /**
  * @return array[]
  * @throws CM_Exception
  */
 private static function _readEmoticonData()
 {
     /** @var CM_File[] $configurationFiles */
     $configurationFiles = [];
     /** @var CM_File[] $imageFiles */
     $imageFiles = [];
     $bootloader = CM_Bootloader::getInstance();
     foreach ($bootloader->getModules() as $namespace) {
         $emoticonDir = new CM_File(CM_Util::getModulePath($namespace) . 'layout/default/resource/img/emoticon/');
         foreach ($emoticonDir->listFiles(true) as $file) {
             $name = strtolower($file->getFileNameWithoutExtension());
             if ('json' === $file->getExtension()) {
                 $configurationFiles[$name] = $file;
             } else {
                 $imageFiles[$name] = $file;
             }
         }
     }
     $dataList = [];
     $codeList = [];
     foreach ($imageFiles as $name => $file) {
         $dataList[$name] = ['name' => $name, 'fileName' => $file->getFileName(), 'codes' => [":{$name}:"]];
         $codeList[":{$name}:"] = $name;
     }
     foreach ($configurationFiles as $name => $file) {
         $additionalCodes = CM_Params::jsonDecode($file->read())['additionalCodes'];
         foreach ($additionalCodes as $code) {
             if (!array_key_exists($code, $codeList)) {
                 $codeList[$code] = $name;
                 $dataList[$name]['codes'][] = $code;
             } else {
                 throw new CM_Exception('Emoticon codes overlap', null, ['overlapping emoticons' => [$name, $codeList[$code]], 'code' => $code]);
             }
         }
     }
     return $dataList;
 }
<?php

require_once CM_Util::getModulePath('CM') . 'library/CM/SmartyPlugins/function.resourceUrl.php';
class smarty_function_resourceUrlTest extends CMTest_TestCase
{
    public function testRender()
    {
        $smarty = new Smarty();
        $render = new CM_Frontend_Render();
        $template = $smarty->createTemplate('string:');
        $template->assignGlobal('render', $render);
        $this->assertSame($render->getUrlResource('layout', 'foo'), smarty_function_resourceUrl(array('path' => 'foo', 'type' => 'layout'), $template));
        $this->assertSame($render->getUrlStatic('foo'), smarty_function_resourceUrl(array('path' => 'foo', 'type' => 'static'), $template));
    }
}
Beispiel #19
0
<?php

require_once CM_Util::getModulePath('CM') . 'library/CM/SmartyPlugins/block.ratioKeeper.php';
class smarty_block_ratioKeeperTest extends CMTest_TestCase
{
    /**
     * @var Smarty_Internal_Template
     */
    private $_template;
    public function setUp()
    {
        $smarty = new Smarty();
        $render = new CM_Frontend_Render();
        $this->_template = $smarty->createTemplate('string:');
        $this->_template->assignGlobal('render', $render);
    }
    public function testRenderRatio()
    {
        $params = ['ratio' => 1];
        $this->_assertContains('padding-bottom: 100%', $params);
        $params = ['ratio' => 0.75];
        $this->_assertContains('padding-bottom: 75%', $params);
        $params = ['ratio' => 0.33];
        $this->_assertContains('padding-bottom: 33%', $params);
        $params = ['height' => 600, 'width' => 900];
        $this->_assertContains('padding-bottom: 66%', $params);
        $params = ['height' => 900, 'width' => 600];
        $this->_assertContains('padding-bottom: 150%', $params);
    }
    public function testRenderContentAttrs()
    {