1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-04 21:57:51 +02:00

Tests for SEF URLs. Search page fix.

This commit is contained in:
Cameron
2021-01-13 16:16:50 -08:00
parent ca471b31db
commit 992b7b4bfe
7 changed files with 534 additions and 22 deletions

View File

@@ -1048,7 +1048,7 @@ class eRouter
protected function _init()
{
// Gather all rules, add-on info, cache, module for main namespace etc
$this->_loadConfig()
$this->loadConfig()
->setAliases();
// we need config first as setter does some checks if module can be set as main
$this->setMainModule(e107::getPref('url_main_module', ''));
@@ -1099,12 +1099,23 @@ class eRouter
* Load config and url rules, if not available - build it on the fly
* @return eRouter
*/
protected function _loadConfig()
public function loadConfig($forceRebuild = false)
{
if(!is_readable(e_CACHE_URL.'config.php')) $config = $this->buildGlobalConfig();
else $config = include(e_CACHE_URL.'config.php');
if(!$config) $config = array();
if(!is_readable(e_CACHE_URL . 'config.php') || $forceRebuild == true)
{
$config = $this->buildGlobalConfig();
}
else
{
$config = include(e_CACHE_URL . 'config.php');
}
if(empty($config))
{
trigger('URL Config is empty', E_USER_NOTICE);
$config = array();
}
$rules = array();
@@ -1115,6 +1126,7 @@ class eRouter
$config[$module] = $config[$module]['config'];
}
$this->_globalConfig = $config;
$this->setRuleSets($rules);
return $this;
@@ -1125,15 +1137,20 @@ class eRouter
if(file_exists(e_CACHE_URL.'config.php'))
{
@unlink(e_CACHE_URL.'config.php');
}
}
}
/**
* Build unified config.php
*/
public function buildGlobalConfig($save = true)
{
$active = e107::getPref('url_config', array());
if(empty($active))
{
trigger_error('url_config pref is empty', E_USER_NOTICE);
}
$config = array();
foreach ($active as $module => $location)
@@ -2257,7 +2274,7 @@ class eRouter
# Modify params if required
if($params)
{
if(varset($config['mapVars']))
if(!empty($config['mapVars']))
{
foreach ($config['mapVars'] as $srcKey => $dstKey)
{
@@ -2266,6 +2283,10 @@ class eRouter
$params[$dstKey] = $params[$srcKey];
unset($params[$srcKey]);
}
else
{
trigger_error("Missing ".$srcKey." during URL creation in ".$module, E_USER_NOTICE);
}
}
}
@@ -2280,7 +2301,14 @@ class eRouter
$params = array();
foreach ($config['allowVars'] as $key)
{
if(isset($copy[$key])) $params[$key] = $copy[$key];
if(isset($copy[$key]))
{
$params[$key] = $copy[$key];
}
else
{
trigger_error("Missing ".$key." during URL creation in ".$module, E_USER_NOTICE);
}
}
unset($copy);
}

View File

@@ -3874,7 +3874,7 @@ class e107
if (empty($sefActive[$plugin])) // SEF disabled.
{
self::getDebug()->log('SEF URL for <b>' . $plugin . '</b> disabled.');
trigger_error('SEF URL for <b>' . $plugin . '</b> disabled.', E_USER_NOTICE);
// trigger_error('SEF URL for ' . $plugin . ' disabled.', E_USER_NOTICE);
$active = false;
}

View File

@@ -579,10 +579,11 @@ class e_pref extends e_front_model
unset($new, $old);
}
// Backup
if($this->set_backup === true && e107::getDb()->gen("REPLACE INTO `#core` (e107_name,e107_value) values ('".$this->prefid."_Backup', '".addslashes($dbdata)."') "))
{
// trigger_error("Performing a pref backup", E_USER_NOTICE);
if(!$disallow_logs) $log->logMessage('Backup of <strong>'.$this->alias.' ('.$this->prefid.')</strong> successfully created.', E_MESSAGE_DEBUG, E_MESSAGE_SUCCESS, $session_messages);
e107::getCache()->clear_sys('Config_'.$this->alias.'_backup');
if(deftrue('e_DEBUG'))
@@ -655,6 +656,7 @@ class e_pref extends e_front_model
->flushMessages('LAN_FIXME', E_LOG_INFORMATIVE, '', $this->prefid);
e107::getMessage()->moveStack($this->prefid);
trigger_error("Settings not saved", E_USER_NOTICE);
return false;
}
else

View File

@@ -27,6 +27,7 @@ class e_search
var $stopwords_php = "|a|about|an|and|are|as|at|be|by|com|edu|for|from|how|i|in|is|it|of|on|or|that|the|this|to|was|what|when|where|who|will|with|the|www|";
var $stopwords_mysql = "|a|a's|able|about|above|according|accordingly|across|actually|after|afterwards|again|against|ain't|all|allow|allows|almost|alone|along|already|also|although|always|am|among|amongst|an|and|another|any|anybody|anyhow|anyone|anything|anyway|anyways|anywhere|apart|appear|appreciate|appropriate|are|aren't|around|as|aside|ask|asking|associated|at|available|away|awfully|be|became|because|become|becomes|becoming|been|before|beforehand|behind|being|believe|below|beside|besides|best|better|between|beyond|both|brief|but|by|c'mon|c's|came|can|can't|cannot|cant|cause|causes|certain|certainly|changes|clearly|co|com|come|comes|concerning|consequently|consider|considering|contain|containing|contains|corresponding|could|couldn't|course|currently|definitely|described|despite|did|didn't|different|do|does|doesn't|doing|don't|done|down|downwards|during|each|edu|eg|eight|either|else|elsewhere|enough|entirely|especially|et|etc|even|ever|every|everybody|everyone|everything|everywhere|ex|exactly|example|except|far|few|fifth|first|five|followed|following|follows|for|former|formerly|forth|four|from|further|furthermore|get|gets|getting|given|gives|go|goes|going|gone|got|gotten|greetings|had|hadn't|happens|hardly|has|hasn't|have|haven't|having|he|he's|hello|help|hence|her|here|here's|hereafter|hereby|herein|hereupon|hers|herself|hi|him|himself|his|hither|hopefully|how|howbeit|however|i|i'd|i'll|i'm|i've|ie|if|ignored|immediate|in|inasmuch|inc|indeed|indicate|indicated|indicates|inner|insofar|instead|into|inward|is|isn't|it|it'd|it'll|it's|its|itself|just|keep|keeps|kept|know|knows|known|last|lately|later|latter|latterly|least|less|lest|let|let's|like|liked|likely|little|look|looking|looks|ltd|mainly|many|may|maybe|me|mean|meanwhile|merely|might|more|moreover|most|mostly|much|must|my|myself|name|namely|nd|near|nearly|necessary|need|needs|neither|never|nevertheless|new|next|nine|no|nobody|non|none|noone|nor|normally|not|nothing|novel|now|nowhere|obviously|of|off|often|oh|ok|okay|old|on|once|one|ones|only|onto|or|other|others|otherwise|ought|our|ours|ourselves|out|outside|over|overall|own|particular|particularly|per|perhaps|php|placed|please|plus|possible|presumably|probably|provides|que|quite|qv|rather|rd|re|really|reasonably|regarding|regardless|regards|relatively|respectively|right|said|same|saw|say|saying|says|second|secondly|see|seeing|seem|seemed|seeming|seems|seen|self|selves|sensible|sent|serious|seriously|seven|several|shall|she|should|shouldn't|since|six|so|some|somebody|somehow|someone|something|sometime|sometimes|somewhat|somewhere|soon|sorry|specified|specify|specifying|still|sub|such|sup|sure|t's|take|taken|tell|tends|th|than|thank|thanks|thanx|that|that's|thats|the|their|theirs|them|themselves|then|thence|there|there's|thereafter|thereby|therefore|therein|theres|thereupon|these|they|they'd|they'll|they're|they've|think|third|this|thorough|thoroughly|those|though|three|through|throughout|thru|thus|to|together|too|took|toward|towards|tried|tries|truly|try|trying|twice|two|un|under|unfortunately|unless|unlikely|until|unto|up|upon|us|use|used|useful|uses|using|usually|value|various|very|via|viz|vs|want|wants|was|wasn't|way|we|we'd|we'll|we're|we've|welcome|well|went|were|weren't|what|what's|whatever|when|whence|whenever|where|where's|whereafter|whereas|whereby|wherein|whereupon|wherever|whether|which|while|whither|who|who's|whoever|whole|whom|whose|why|will|willing|wish|with|within|without|won't|wonder|would|would|wouldn't|yes|yet|you|you'd|you'll|you're|you've|your|yours|yourself|yourselves|zero|";
var $params;
public $stop_keys = array();
function __construct($query = '')
{

View File

@@ -27,7 +27,6 @@ class e107Test extends \Codeception\Test\Unit
$this->fail("Couldn't load e107 object");
}
// var_dump($this->e107);
}
public function testGetInstance()
@@ -50,6 +49,8 @@ class e107Test extends \Codeception\Test\Unit
$this->assertEquals('000000test', $res->site_path);
$this->assertEquals('/', e_HTTP);
}
public function testRenderLayout()
@@ -887,14 +888,293 @@ class e107Test extends \Codeception\Test\Unit
$this->assertTrue($res);
}
*/
private function generateRows($var, $plugin )
{
preg_match_all('#\{([a-z_]*)\}#', $var['sef'], $matches);
$variables = array('-one-', '-two-', '-three-');
$ret = [];
if(!empty($matches[1]))
{
$c = 0;
foreach($matches[1] as $v)
{
if($v === 'alias' && !empty($var['alias']))
{
$ret['alias'] = $var['alias'];
}
else
{
$ret[$v] = $variables[$c];
$c++;
}
}
}
/*else
{
echo "\n".$plugin.' had no matches for: '.varset($var['sef'])."\n";
}*/
return $ret;
}
private function generateExpected($string, $rows)
{
$search = array('&');;
$replace = array('&amp;');
foreach($rows as $k=>$v)
{
$search[] = '{'.$k.'}';
$replace[] = $v;
}
return SITEURL.str_replace($search, $replace, $string);
}
public function testUrl()
{
$obj = $this->e107;
$result = $obj::url('news', 'index', array(), array('mode' => 'full'));
$tests = array(
0 => array(
'plugin' => 'news',
'key' => 'index',
'row' => '',
'options' => ['mode' => 'full'],
'_expected_' => 'https://localhost/e107/news'
),
);
$tests = array();
$all = e107::getAddonConfig('e_url');
foreach($all as $plugin=>$var)
{
foreach($var as $key=>$value)
{
$rows = $this->generateRows($value, $plugin);
$tests[] = array(
'plugin' => $plugin,
'key' => $key,
'row' => $rows,
'options' => ['mode' => 'full'],
'_expected_' => $this->generateExpected($value['sef'], $rows),
);
}
}
foreach($tests as $index => $var)
{
if(empty($var['plugin']))
{
continue;
}
$result = $obj::url($var['plugin'], $var['key'], $var['row'], $var['options']);
if(empty($var['_expected_']))
{
echo $result."\n";
continue;
}
$this->assertEquals($var['_expected_'], $result);
// $this->assertEquals("https://localhost/e107/news", $result);
}
}
/**
* /*
* e107::getUrl()->create('page/book/index', $row,'allow=chapter_id,chapter_sef,book_sef') ;
e107::getUrl()->create('user/profile/view', $this->news_item)
e107::getUrl()->create('user/profile/view', array('name' => $this->var['user_name'], 'id' => $this->var['user_id']));
e107::getUrl()->create('page/chapter/index', $row,'allow=chapter_id,chapter_sef,book_sef') ;
e107::getUrl()->create('user/myprofile/edit');
e107::getUrl()->create('gallery/index/list', $this->var);
e107::getUrl()->create('news/view/item', $row, array('full' => 1));
e107::getUrl()->create('news/list/all'),
e107::getUrl()->create('page/view/index',$row),
e107::getUrl()->create('page/chapter/index', $sef),
($sef = $row;
$sef['chapter_sef'] = $this->getSef($row['chapter_id']);
$sef['book_sef'] = $this->getSef($row['chapter_parent']);)
e107::getUrl()->create('news/list/tag', array('tag' => $word));
$LINKTOFORUM = e107::getUrl()->create('forum/forum/view', array('id' => $row['thread_forum_id'])); //$e107->url->getUrl('forum', 'forum', "func=view&id={$row['thread_forum_id']}");
e107::getUrl()->create('search');
*/
public function testUrlLegacy()
{
// set eURL config to 'Friendly'
$oldConfig = e107::getPref('url_config');
$newConfig = array(
'news' => 'core/sef_full',
'page' => 'core/sef_chapters',
'search' => 'core/rewrite',
'system' => 'core/rewrite',
'user' => 'core/rewrite',
'gallery' => 'plugin/rewrite'
);
$this->setUrlConfig($newConfig);
$legacyTests = array(
0 => array(
'route' => 'news/view/item',
'row' => array('news_id' => 1, 'news_sef'=>'my-news-item', 'category_sef'=>'my-category'),
'options' => 'full=1',
'_expected_' => 'https://localhost/e107/news/my-category/my-news-item'
),
1 => array(
'route' => 'news/view/item',
'row' => array('id' => 1, 'name'=>'my-news-item', 'category'=>'my-category'),
'options' => 'full=1',
'_expected_' => 'https://localhost/e107/news/my-category/my-news-item'
),
2 => array(
'route' => 'news/list/short',
'row' => array('id' => 1, 'name'=>'my-news-item', 'category'=>'my-category'),
'options' => 'full=1',
'_expected_' => 'https://localhost/e107/news/short/my-news-item'
),
3 => array(
'route' => 'news/list/tag',
'row' => array('tag'=>'myword'),
'options' => 'full=1',
'_expected_' => 'https://localhost/e107/news/tag/myword'
),
4 => array(
'route' => 'search',
'row' => '',
'options' => 'full=1',
'_expected_' => 'https://localhost/e107/search'
),
5 => array(
'route' => 'user/profile/view',
'row' => array('user_id' => 3, 'user_name'=>'john'),
'options' => 'full=1',
'_expected_' => 'https://localhost/e107/user/john'
),
6 => array(
'route' => 'page/book/index',
'row' => array('chapter_id'=>2,'chapter_sef'=>'my-book'),
'options' => 'full=1',
'_expected_' => 'https://localhost/e107/page/my-book'
),
7 => array(
'route' => 'page/chapter/index',
'row' => array('chapter_id'=>2,'chapter_sef'=>'my-chapter','book_sef'=>'my-book'),
'options' => 'full=1',
'_expected_' => 'https://localhost/e107/page/my-book/my-chapter'
),
8 => array(
'route' => 'page/view',
'row' => array('page_id'=>3, 'page_sef'=>'my-page','chapter_id'=>2,'chapter_sef'=>'my-chapter','book_sef'=>'my-book'),
'options' => 'full=1',
'_expected_' => 'https://localhost/e107/page/my-book/my-chapter/my-page'
),
// todo add more.
);
$e107 = $this->e107;
foreach($legacyTests as $index => $var)
{
if(empty($var['route']))
{
continue;
}
$result = $e107::url($var['route'], $var['row'], $var['options']);
$lresult = e107::getUrl()->create($var['route'], $var['row'], $var['options']);
if(empty($var['_expected_']))
{
echo $result."\n";
echo $lresult."\n\n";
continue;
}
$this->assertEquals($result, $lresult, "Legacy Test #".$index." -- e107::getUrl()->create('".$var['route']."') didn't match e107::url('".$var['route']."')" );
$this->assertEquals($var['_expected_'], $result, 'Legacy URL index #'.$index.' failed');
}
$this->setUrlConfig($oldConfig); // return config to previous state.
}
/**
* Save the url_config preference
* @param array $newConfig
*/
private function setUrlConfig($newConfig=array())
{
if(empty($newConfig))
{
return null;
}
$cfg = e107::getConfig();
foreach($newConfig as $k => $v)
{
$cfg->setPref('url_config/' . $k, $v);
}
$cfg->save(false, true);
/** @var eRouter $router */
$router = e107::getUrl()->router(); // e107::getSingleton('eRouter');
$rules = $router->getRuleSets();
if(empty($rules['news']) || empty($rules['page']))
{
$router->loadConfig(true);
}
$this->assertEquals("https://localhost/e107/news", $result);
}
/**

View File

@@ -858,11 +858,217 @@ while(&#036;row = &#036;sql-&gt;fetch())
public function testGetAllowedAttributes()
{
$expected = array (
'default' =>
array (
0 => 'id',
1 => 'style',
2 => 'class',
3 => 'title',
4 => 'lang',
5 => 'accesskey',
),
'img' =>
array (
0 => 'src',
1 => 'alt',
2 => 'width',
3 => 'height',
4 => 'id',
5 => 'style',
6 => 'class',
7 => 'title',
8 => 'lang',
9 => 'accesskey',
),
'a' =>
array (
0 => 'href',
1 => 'target',
2 => 'rel',
3 => 'id',
4 => 'style',
5 => 'class',
6 => 'title',
7 => 'lang',
8 => 'accesskey',
),
'script' =>
array (
0 => 'type',
1 => 'src',
2 => 'language',
3 => 'async',
4 => 'id',
5 => 'style',
6 => 'class',
7 => 'title',
8 => 'lang',
9 => 'accesskey',
),
'iframe' =>
array (
0 => 'src',
1 => 'frameborder',
2 => 'width',
3 => 'height',
4 => 'id',
5 => 'style',
6 => 'class',
7 => 'title',
8 => 'lang',
9 => 'accesskey',
),
'input' =>
array (
0 => 'type',
1 => 'name',
2 => 'value',
3 => 'id',
4 => 'style',
5 => 'class',
6 => 'title',
7 => 'lang',
8 => 'accesskey',
),
'form' =>
array (
0 => 'action',
1 => 'method',
2 => 'target',
3 => 'id',
4 => 'style',
5 => 'class',
6 => 'title',
7 => 'lang',
8 => 'accesskey',
),
'audio' =>
array (
0 => 'src',
1 => 'controls',
2 => 'autoplay',
3 => 'loop',
4 => 'muted',
5 => 'preload',
6 => 'id',
7 => 'style',
8 => 'class',
9 => 'title',
10 => 'lang',
11 => 'accesskey',
),
'video' =>
array (
0 => 'autoplay',
1 => 'controls',
2 => 'height',
3 => 'loop',
4 => 'muted',
5 => 'poster',
6 => 'preload',
7 => 'src',
8 => 'width',
9 => 'id',
10 => 'style',
11 => 'class',
12 => 'title',
13 => 'lang',
14 => 'accesskey',
),
'td' =>
array (
0 => 'colspan',
1 => 'rowspan',
2 => 'id',
3 => 'style',
4 => 'class',
5 => 'title',
6 => 'lang',
7 => 'accesskey',
),
'th' =>
array (
0 => 'colspan',
1 => 'rowspan',
2 => 'id',
3 => 'style',
4 => 'class',
5 => 'title',
6 => 'lang',
7 => 'accesskey',
),
'col' =>
array (
0 => 'span',
1 => 'id',
2 => 'style',
3 => 'class',
4 => 'title',
5 => 'lang',
6 => 'accesskey',
),
'embed' =>
array (
0 => 'src',
1 => 'wmode',
2 => 'type',
3 => 'width',
4 => 'height',
5 => 'id',
6 => 'style',
7 => 'class',
8 => 'title',
9 => 'lang',
10 => 'accesskey',
),
'x-bbcode' =>
array (
0 => 'alt',
1 => 'id',
2 => 'style',
3 => 'class',
4 => 'title',
5 => 'lang',
6 => 'accesskey',
),
'label' =>
array (
0 => 'for',
1 => 'id',
2 => 'style',
3 => 'class',
4 => 'title',
5 => 'lang',
6 => 'accesskey',
),
'source' =>
array (
0 => 'media',
1 => 'sizes',
2 => 'src',
3 => 'srcset',
4 => 'type',
5 => 'id',
6 => 'style',
7 => 'class',
8 => 'title',
9 => 'lang',
10 => 'accesskey',
),
);
$result = $this->tp->getAllowedAttributes();
$this->assertSame($expected, $result);
$true = is_array($result) && in_array('style',$result['img']);
$this->assertTrue($true);
// var_export($result);
// $true = is_array($result) && in_array('style',$result['img']);
// $this->assertTrue($true);
}
/*
public function testSetScriptTags()

View File

@@ -1471,11 +1471,6 @@ class e_parse_shortcodeTest extends \Codeception\Test\Unit
// echo "\n".$shortcode."\n";
$result = $tp->parseTemplate($shortcode,true);
if($name === 'search')
{
echo $result."\n\n";
}
if(isset($parms[$name]) && $name !== 'user_extended')
{
$this->assertNotEmpty($result, $shortcode." returned nothing!");