2018-07-01 13:45:30 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Test
|
|
|
|
* @group AbuseFilter
|
2019-05-25 12:10:07 +00:00
|
|
|
* @group AbuseFilterParser
|
2018-07-01 13:45:30 +00:00
|
|
|
*
|
|
|
|
* @covers AbuseFilterTokenizer
|
2018-08-22 11:27:50 +00:00
|
|
|
* @covers AFPToken
|
|
|
|
* @covers AbuseFilterParser
|
2019-05-25 12:10:07 +00:00
|
|
|
* @covers AbuseFilterCachingParser
|
|
|
|
* @covers AFPTreeParser
|
|
|
|
* @covers AFPTreeNode
|
2018-08-22 11:27:50 +00:00
|
|
|
* @covers AFPUserVisibleException
|
2018-08-22 16:50:15 +00:00
|
|
|
* @covers AFPException
|
2018-07-01 13:45:30 +00:00
|
|
|
*/
|
2018-08-22 14:33:35 +00:00
|
|
|
class AbuseFilterTokenizerTest extends AbuseFilterParserTestCase {
|
2018-07-01 13:45:30 +00:00
|
|
|
/**
|
|
|
|
* Test the 'unclosedcomment' exception
|
|
|
|
*
|
|
|
|
* @param string $expr The expression to test
|
|
|
|
* @param string $caller The function where the exception is thrown
|
|
|
|
* @covers AbuseFilterTokenizer::nextToken
|
|
|
|
* @dataProvider unclosedComment
|
|
|
|
*/
|
|
|
|
public function testUnclosedCommentException( $expr, $caller ) {
|
|
|
|
$this->exceptionTest( 'unclosedcomment', $expr, $caller );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testUnclosedCommentException
|
|
|
|
* The second parameter is the function where the exception is raised.
|
|
|
|
* One expression for each throw.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function unclosedComment() {
|
|
|
|
return [
|
|
|
|
[ ' /**** / * /', 'nextToken' ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the 'unrecognisedtoken' exception
|
|
|
|
*
|
|
|
|
* @param string $expr The expression to test
|
|
|
|
* @param string $caller The function where the exception is thrown
|
|
|
|
* @covers AbuseFilterTokenizer::nextToken
|
|
|
|
* @dataProvider unrecognisedToken
|
|
|
|
*/
|
|
|
|
public function testUnrecognisedTokenException( $expr, $caller ) {
|
|
|
|
$this->exceptionTest( 'unrecognisedtoken', $expr, $caller );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testUnrecognisedTokenException
|
|
|
|
* The second parameter is the function where the exception is raised.
|
|
|
|
* One expression for each throw.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function unrecognisedToken() {
|
|
|
|
return [
|
|
|
|
[ '#', 'nextToken' ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the 'unclosedstring' exception
|
|
|
|
*
|
|
|
|
* @param string $expr The expression to test
|
|
|
|
* @param string $caller The function where the exception is thrown
|
|
|
|
* @covers AbuseFilterTokenizer::readStringLiteral
|
|
|
|
* @dataProvider unclosedString
|
|
|
|
*/
|
|
|
|
public function testUnclosedStringException( $expr, $caller ) {
|
|
|
|
$this->exceptionTest( 'unclosedstring', $expr, $caller );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testUnclosedStringException
|
|
|
|
* The second parameter is the function where the exception is raised.
|
|
|
|
* One expression for each throw.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function unclosedString() {
|
|
|
|
return [
|
|
|
|
[ '"', 'readStringLiteral' ],
|
|
|
|
];
|
|
|
|
}
|
2019-04-14 08:59:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that tokenized code is saved in cache
|
|
|
|
*
|
|
|
|
* @param string $code To be tokenized
|
|
|
|
* @dataProvider provideCode
|
|
|
|
*/
|
|
|
|
public function testCaching( $code ) {
|
2019-05-28 08:11:03 +00:00
|
|
|
$cache = new HashBagOStuff();
|
|
|
|
$this->setService( 'LocalServerObjectCache', $cache );
|
2019-04-23 17:08:44 +00:00
|
|
|
|
|
|
|
$key = AbuseFilterTokenizer::getCacheKey( $cache, $code );
|
2019-04-14 08:59:39 +00:00
|
|
|
|
|
|
|
// Other tests may have already cached the same code.
|
|
|
|
$cache->delete( $key );
|
2019-04-23 17:08:44 +00:00
|
|
|
AbuseFilterTokenizer::getTokens( $code );
|
2019-05-28 08:11:03 +00:00
|
|
|
$this->assertNotFalse( $cache->get( $key ) );
|
2019-04-14 08:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider for testCaching
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideCode() {
|
|
|
|
return [
|
|
|
|
[ '1 === 1' ],
|
|
|
|
[ 'added_lines irlike "test"' ],
|
|
|
|
[ 'edit_delta > 57 & action === "edit"' ],
|
|
|
|
[ '!("confirmed") in user_groups' ]
|
|
|
|
];
|
|
|
|
}
|
2018-07-01 13:45:30 +00:00
|
|
|
}
|