1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-06-08 15:35:11 +02:00

Merge remote-tracking branch 'p/ticket/11174' into develop

* p/ticket/11174:
  [ticket/11174] Global $cache is a cache service instance.
  [ticket/11174] Delete more copy pasting.
  [ticket/11174] Drop needless teardown functions.
  [ticket/11174] These tests do not need posts fixtures.
  [ticket/11174] Empty fixture for when we don't need any data.
  [ticket/11174] Eliminate search wrapper copy pasting.
  [ticket/11174] Extract phpbb_search_test_case.
  [ticket/11174] Delete copy pasting.
  [ticket/11174] Move assertion definition to base class.
  [ticket/11174] add unit tests for postgres search backend
  [ticket/11174] include utf_tools in postgres search backend
  [ticket/11174] negation queries do not return false
  [ticket/11174] set config values
  [ticket/11174] add test case for native test
  [ticket/11174] rename native wrapper class
  [ticket/11174] add mysql unit tests
  [ticket/11174] removes unnecessary space from word
  [ticket/11174] include utf_tools in mysql backend
This commit is contained in:
Andreas Fischer 2012-11-29 18:55:56 +01:00
commit aaacfae428
9 changed files with 265 additions and 41 deletions

View File

@ -86,6 +86,14 @@ class phpbb_search_fulltext_mysql extends phpbb_search_base
$this->word_length = array('min' => $this->config['fulltext_mysql_min_word_len'], 'max' => $this->config['fulltext_mysql_max_word_len']);
/**
* Load the UTF tools
*/
if (!function_exists('utf8_strlen'))
{
include($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
}
$error = false;
}
@ -230,7 +238,7 @@ class phpbb_search_fulltext_mysql extends phpbb_search_base
}
else
{
$tmp_split_words[] = $word . ' ';
$tmp_split_words[] = $word;
}
}
if ($phrase)

View File

@ -121,6 +121,14 @@ class phpbb_search_fulltext_postgres extends phpbb_search_base
}
}
/**
* Load the UTF tools
*/
if (!function_exists('utf8_strlen'))
{
include($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
}
$error = false;
}

5
tests/fixtures/empty.xml vendored Normal file
View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<table name="phpbb_posts">
</table>
</dataset>

View File

@ -0,0 +1,106 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/../test_framework/phpbb_search_test_case.php';
abstract class phpbb_search_common_test_case extends phpbb_search_test_case
{
public function keywords()
{
return array(
// keywords
// terms
// ok
// split words
// common words
array(
'fooo',
'all',
true,
array('fooo'),
array(),
),
array(
'fooo baar',
'all',
true,
array('fooo', 'baar'),
array(),
),
// leading, trailing and multiple spaces
array(
' fooo baar ',
'all',
true,
array('fooo', 'baar'),
array(),
),
// words too short
array(
'f',
'all',
false,
null,
// short words count as "common" words
array('f'),
),
array(
'f o o',
'all',
false,
null,
array('f', 'o', 'o'),
),
array(
'f -o -o',
'all',
false,
null,
array('f', '-o', '-o'),
),
array(
'fooo -baar',
'all',
true,
array('-baar', 'fooo'),
array(),
),
// all negative
array(
'-fooo',
'all',
true,
array('-fooo'),
array(),
),
array(
'-fooo -baar',
'all',
true,
array('-fooo', '-baar'),
array(),
),
);
}
/**
* @dataProvider keywords
*/
public function test_split_keywords($keywords, $terms, $ok, $split_words, $common)
{
$rv = $this->search->split_keywords($keywords, $terms);
$this->assertEquals($ok, $rv);
if ($ok)
{
// only check criteria if the search is going to be performed
$this->assert_array_content_equals($split_words, $this->search->get_split_words());
}
$this->assert_array_content_equals($common, $this->search->get_common_words());
}
}

View File

@ -0,0 +1,40 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/common_test_case.php';
class phpbb_search_mysql_test extends phpbb_search_common_test_case
{
protected $db;
protected $search;
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/../fixtures/empty.xml');
}
protected function setUp()
{
global $phpbb_root_path, $phpEx, $config, $user, $cache;
parent::setUp();
// dbal uses cache
$cache = new phpbb_cache_service(new phpbb_cache_driver_null);
// set config values
$config['fulltext_mysql_min_word_len'] = 4;
$config['fulltext_mysql_max_word_len'] = 254;
$this->db = $this->new_dbal();
$error = null;
$class = self::get_search_wrapper('phpbb_search_fulltext_mysql');
$this->search = new $class($error, $phpbb_root_path, $phpEx, null, $config, $this->db, $user);
}
}

View File

@ -7,24 +7,9 @@
*
*/
function phpbb_search_wrapper($class)
{
$wrapped = $class . '_wrapper';
if (!class_exists($wrapped))
{
$code = "
class $wrapped extends $class
{
public function get_must_contain_ids() { return \$this->must_contain_ids; }
public function get_must_not_contain_ids() { return \$this->must_not_contain_ids; }
}
";
eval($code);
}
return $wrapped;
}
require_once dirname(__FILE__) . '/../test_framework/phpbb_search_test_case.php';
class phpbb_search_native_test extends phpbb_database_test_case
class phpbb_search_native_test extends phpbb_search_test_case
{
protected $db;
protected $search;
@ -41,19 +26,14 @@ class phpbb_search_native_test extends phpbb_database_test_case
parent::setUp();
// dbal uses cache
$cache = new phpbb_cache_driver_null;
$cache = new phpbb_cache_service(new phpbb_cache_driver_null);
$this->db = $this->new_dbal();
$error = null;
$class = phpbb_search_wrapper('phpbb_search_fulltext_native');
$class = self::get_search_wrapper('phpbb_search_fulltext_native');
$this->search = new $class($error, $phpbb_root_path, $phpEx, null, $config, $this->db, $user);
}
protected function tearDown()
{
parent::tearDown();
}
public function keywords()
{
return array(
@ -106,6 +86,14 @@ class phpbb_search_native_test extends phpbb_database_test_case
null,
array('f', 'o', 'o'),
),
array(
'f -o -o',
'all',
false,
null,
null,
array('f', 'o', 'o'),
),
array(
'foo -bar',
'all',
@ -167,20 +155,4 @@ class phpbb_search_native_test extends phpbb_database_test_case
}
$this->assert_array_content_equals($common, $this->search->get_common_words());
}
public function assert_array_content_equals($one, $two)
{
// http://stackoverflow.com/questions/3838288/phpunit-assert-two-arrays-are-equal-but-order-of-elements-not-important
// but one array_diff is not enough!
if (sizeof(array_diff($one, $two)) || sizeof(array_diff($two, $one)))
{
// get a nice error message
$this->assertEquals($one, $two);
}
else
{
// increase assertion count
$this->assertTrue(true);
}
}
}

View File

@ -0,0 +1,40 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/common_test_case.php';
class phpbb_search_postgres_test extends phpbb_search_common_test_case
{
protected $db;
protected $search;
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/../fixtures/empty.xml');
}
protected function setUp()
{
global $phpbb_root_path, $phpEx, $config, $user, $cache;
parent::setUp();
// dbal uses cache
$cache = new phpbb_cache_service(new phpbb_cache_driver_null);
// set config values
$config['fulltext_postgres_min_word_len'] = 4;
$config['fulltext_postgres_max_word_len'] = 254;
$this->db = $this->new_dbal();
$error = null;
$class = self::get_search_wrapper('phpbb_search_fulltext_postgres');
$this->search = new $class($error, $phpbb_root_path, $phpEx, null, $config, $this->db, $user);
}
}

View File

@ -141,4 +141,20 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
{
return $matches[1] . strtoupper($matches[2]) . $matches[3];
}
public function assert_array_content_equals($one, $two)
{
// http://stackoverflow.com/questions/3838288/phpunit-assert-two-arrays-are-equal-but-order-of-elements-not-important
// but one array_diff is not enough!
if (sizeof(array_diff($one, $two)) || sizeof(array_diff($two, $one)))
{
// get a nice error message
$this->assertEquals($one, $two);
}
else
{
// increase assertion count
$this->assertTrue(true);
}
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
abstract class phpbb_search_test_case extends phpbb_database_test_case
{
static protected function get_search_wrapper($class)
{
$wrapped = $class . '_wrapper';
if (!class_exists($wrapped))
{
$code = "
class $wrapped extends $class
{
public function get_must_contain_ids() { return \$this->must_contain_ids; }
public function get_must_not_contain_ids() { return \$this->must_not_contain_ids; }
public function get_split_words() { return \$this->split_words; }
}
";
eval($code);
}
return $wrapped;
}
}