aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2015-02-17 13:05:55 +0100
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2015-02-17 13:05:55 +0100
commitc0aba0a371f408ada8dec3e5c8f7fb177ceac3f8 (patch)
tree0dfbe485d5f05e79cffa5ef83b6323e4c0b5dfa9
parent4bb8548eccf22b40c25352fe27c66f1f8039ebcd (diff)
parent964e67d4a4914008b0f7bc941d77215b2038012c (diff)
Merge pull request #792 from aledeg/extract-search-from-context
Extract search from context
-rw-r--r--app/Models/Context.php148
-rw-r--r--app/Models/Search.php195
-rw-r--r--tests/app/Models/ContextTest.php236
-rw-r--r--tests/app/Models/SearchTest.php277
4 files changed, 472 insertions, 384 deletions
diff --git a/app/Models/Context.php b/app/Models/Context.php
index 645639907..f00bb1e97 100644
--- a/app/Models/Context.php
+++ b/app/Models/Context.php
@@ -302,152 +302,4 @@ class FreshRSS_Context {
return false;
}
- /**
- * Parse search string to extract the different keywords.
- *
- * @return array
- */
- public function parseSearch() {
- $search = self::$search;
- $intitle = $this->parseIntitleSearch($search);
- $author = $this->parseAuthorSearch($intitle['string']);
- $inurl = $this->parseInurlSearch($author['string']);
- $pubdate = $this->parsePubdateSearch($inurl['string']);
- $date = $this->parseDateSearch($pubdate['string']);
-
- $remaining = array();
- $remaining_search = trim($date['string']);
- if (strcmp($remaining_search, '') != 0) {
- $remaining['search'] = $remaining_search;
- }
-
- return array_merge($intitle['search'], $author['search'], $inurl['search'], $date['search'], $pubdate['search'], $remaining);
- }
-
- /**
- * Parse the search string to find intitle keyword and the search related
- * to it.
- * The search is the first word following the keyword.
- * It returns an array containing the matched string and the search.
- *
- * @param string $search
- * @return array
- */
- private function parseIntitleSearch($search) {
- if (preg_match('/intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $search, $matches)) {
- return array(
- 'string' => str_replace($matches[0], '', $search),
- 'search' => array('intitle' => $matches['search']),
- );
- }
- if (preg_match('/intitle:(?P<search>\w*)/', $search, $matches)) {
- return array(
- 'string' => str_replace($matches[0], '', $search),
- 'search' => array('intitle' => $matches['search']),
- );
- }
- return array(
- 'string' => $search,
- 'search' => array(),
- );
- }
-
- /**
- * Parse the search string to find author keyword and the search related
- * to it.
- * The search is the first word following the keyword except when using
- * a delimiter. Supported delimiters are single quote (') and double
- * quotes (").
- * It returns an array containing the matched string and the search.
- *
- * @param string $search
- * @return array
- */
- private function parseAuthorSearch($search) {
- if (preg_match('/author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $search, $matches)) {
- return array(
- 'string' => str_replace($matches[0], '', $search),
- 'search' => array('author' => $matches['search']),
- );
- }
- if (preg_match('/author:(?P<search>\w*)/', $search, $matches)) {
- return array(
- 'string' => str_replace($matches[0], '', $search),
- 'search' => array('author' => $matches['search']),
- );
- }
- return array(
- 'string' => $search,
- 'search' => array(),
- );
- }
-
- /**
- * Parse the search string to find inurl keyword and the search related
- * to it.
- * The search is the first word following the keyword except.
- * It returns an array containing the matched string and the search.
- *
- * @param string $search
- * @return array
- */
- private function parseInurlSearch($search) {
- if (preg_match('/inurl:(?P<search>[^\s]*)/', $search, $matches)) {
- return array(
- 'string' => str_replace($matches[0], '', $search),
- 'search' => array('inurl' => $matches['search']),
- );
- }
- return array(
- 'string' => $search,
- 'search' => array(),
- );
- }
-
- /**
- * Parse the search string to find date keyword and the search related
- * to it.
- * The search is the first word following the keyword.
- * It returns an array containing the matched string and the search.
- *
- * @param string $search
- * @return array
- */
- private function parseDateSearch($search) {
- if (preg_match('/date:(?P<search>[^\s]*)/', $search, $matches)) {
- list($min_date, $max_date) = parseDateInterval($matches['search']);
- return array(
- 'string' => str_replace($matches[0], '', $search),
- 'search' => array('min_date' => $min_date, 'max_date' => $max_date),
- );
- }
- return array(
- 'string' => $search,
- 'search' => array(),
- );
- }
-
- /**
- * Parse the search string to find pubdate keyword and the search related
- * to it.
- * The search is the first word following the keyword.
- * It returns an array containing the matched string and the search.
- *
- * @param string $search
- * @return array
- */
- private function parsePubdateSearch($search) {
- if (preg_match('/pubdate:(?P<search>[^\s]*)/', $search, $matches)) {
- list($min_date, $max_date) = parseDateInterval($matches['search']);
- return array(
- 'string' => str_replace($matches[0], '', $search),
- 'search' => array('min_pubdate' => $min_date, 'max_pubdate' => $max_date),
- );
- }
- return array(
- 'string' => $search,
- 'search' => array(),
- );
- }
-
}
diff --git a/app/Models/Search.php b/app/Models/Search.php
new file mode 100644
index 000000000..ef8fc883d
--- /dev/null
+++ b/app/Models/Search.php
@@ -0,0 +1,195 @@
+<?php
+
+/**
+ * Contains a search from the search form.
+ *
+ * It allows to extract meaningful bits of the search and store them in a
+ * convenient object
+ */
+class FreshRSS_Search {
+
+ // This contains the user input string
+ private $raw_input;
+ // The following properties are extracted from the raw input
+ private $intitle;
+ private $min_date;
+ private $max_date;
+ private $min_pubdate;
+ private $max_pubdate;
+ private $inurl;
+ private $author;
+ private $tags;
+ private $search;
+
+ public function __construct($input) {
+ if (strcmp($input, '') == 0) {
+ return;
+ }
+ $this->raw_input = $input;
+ $input = $this->parseIntitleSearch($input);
+ $input = $this->parseAuthorSearch($input);
+ $input = $this->parseInurlSearch($input);
+ $input = $this->parsePubdateSearch($input);
+ $input = $this->parseDateSearch($input);
+ $input = $this->parseTagsSeach($input);
+ $this->search = $this->cleanSearch($input);
+ }
+
+ public function getRawInput() {
+ return $this->raw_input;
+ }
+
+ public function getIntitle() {
+ return $this->intitle;
+ }
+
+ public function getMinDate() {
+ return $this->min_date;
+ }
+
+ public function getMaxDate() {
+ return $this->max_date;
+ }
+
+ public function getMinPubdate() {
+ return $this->min_pubdate;
+ }
+
+ public function getMaxPubdate() {
+ return $this->max_pubdate;
+ }
+
+ public function getInurl() {
+ return $this->inurl;
+ }
+
+ public function getAuthor() {
+ return $this->author;
+ }
+
+ public function getTags() {
+ return $this->tags;
+ }
+
+ public function getSearch() {
+ return $this->search;
+ }
+
+ /**
+ * Parse the search string to find intitle keyword and the search related
+ * to it.
+ * The search is the first word following the keyword.
+ *
+ * @param string $input
+ * @return string
+ */
+ private function parseIntitleSearch($input) {
+ if (preg_match('/intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
+ $this->intitle = $matches['search'];
+ return str_replace($matches[0], '', $input);
+ }
+ if (preg_match('/intitle:(?P<search>\w*)/', $input, $matches)) {
+ $this->intitle = $matches['search'];
+ return str_replace($matches[0], '', $input);
+ }
+ return $input;
+ }
+
+ /**
+ * Parse the search string to find author keyword and the search related
+ * to it.
+ * The search is the first word following the keyword except when using
+ * a delimiter. Supported delimiters are single quote (') and double
+ * quotes (").
+ *
+ * @param string $input
+ * @return string
+ */
+ private function parseAuthorSearch($input) {
+ if (preg_match('/author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
+ $this->author = $matches['search'];
+ return str_replace($matches[0], '', $input);
+ }
+ if (preg_match('/author:(?P<search>\w*)/', $input, $matches)) {
+ $this->author = $matches['search'];
+ return str_replace($matches[0], '', $input);
+ }
+ return $input;
+ }
+
+ /**
+ * Parse the search string to find inurl keyword and the search related
+ * to it.
+ * The search is the first word following the keyword except.
+ *
+ * @param string $input
+ * @return string
+ */
+ private function parseInurlSearch($input) {
+ if (preg_match('/inurl:(?P<search>[^\s]*)/', $input, $matches)) {
+ $this->inurl = $matches['search'];
+ return str_replace($matches[0], '', $input);
+ }
+ return $input;
+ }
+
+ /**
+ * Parse the search string to find date keyword and the search related
+ * to it.
+ * The search is the first word following the keyword.
+ *
+ * @param string $input
+ * @return string
+ */
+ private function parseDateSearch($input) {
+ if (preg_match('/date:(?P<search>[^\s]*)/', $input, $matches)) {
+ list($this->min_date, $this->max_date) = parseDateInterval($matches['search']);
+ return str_replace($matches[0], '', $input);
+ }
+ return $input;
+ }
+
+ /**
+ * Parse the search string to find pubdate keyword and the search related
+ * to it.
+ * The search is the first word following the keyword.
+ *
+ * @param string $input
+ * @return string
+ */
+ private function parsePubdateSearch($input) {
+ if (preg_match('/pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
+ list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($matches['search']);
+ return str_replace($matches[0], '', $input);
+ }
+ return $input;
+ }
+
+ /**
+ * Parse the search string to find tags keyword (# followed by a word)
+ * and the search related to it.
+ * The search is the first word following the #.
+ *
+ * @param string $input
+ * @return string
+ */
+ private function parseTagsSeach($input) {
+ if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
+ $this->tags = $matches['search'];
+ return str_replace($matches[0], '', $input);
+ }
+ return $input;
+ }
+
+ /**
+ * Remove all unnecessary spaces in the search
+ *
+ * @param string $input
+ * @return string
+ */
+ private function cleanSearch($input) {
+ $input = preg_replace('/\s+/', ' ', $input);
+ return trim($input);
+ }
+
+}
diff --git a/tests/app/Models/ContextTest.php b/tests/app/Models/ContextTest.php
index c5da6f667..4dc8b7757 100644
--- a/tests/app/Models/ContextTest.php
+++ b/tests/app/Models/ContextTest.php
@@ -1,241 +1,5 @@
<?php
-require_once(LIB_PATH . '/lib_date.php');
-
class ContextTest extends \PHPUnit_Framework_TestCase {
- private $context;
-
- public function setUp() {
- $this->context = new FreshRSS_Context();
- }
-
- public function testParseSearch_whenEmpty_returnsEmptyArray() {
- $this->assertCount(0, $this->context->parseSearch());
- }
-
- /**
- * @dataProvider provideMultipleKeywordSearch
- * @param string $search
- * @param string $expected_values
- */
- public function testParseSearch_whenMultipleKeywords_returnArrayWithMultipleValues($search, $expected_values) {
- FreshRSS_Context::$search = $search;
- $parsed_search = $this->context->parseSearch();
- $this->assertEquals($expected_values, $parsed_search);
- }
-
- /**
- * @return array
- */
- public function provideMultipleKeywordSearch() {
- return array(
- array(
- 'intitle:word1 author:word2',
- array(
- 'intitle' => 'word1',
- 'author' => 'word2',
- ),
- ),
- array(
- 'author:word2 intitle:word1',
- array(
- 'intitle' => 'word1',
- 'author' => 'word2',
- ),
- ),
- array(
- 'author:word1 inurl:word2',
- array(
- 'author' => 'word1',
- 'inurl' => 'word2',
- ),
- ),
- array(
- 'inurl:word2 author:word1',
- array(
- 'author' => 'word1',
- 'inurl' => 'word2',
- ),
- ),
- array(
- 'date:2008-01-01/2008-02-01 pubdate:2007-01-01/2007-02-01',
- array(
- 'min_date' => '1199163600',
- 'max_date' => '1201928399',
- 'min_pubdate' => '1167627600',
- 'max_pubdate' => '1170392399',
- ),
- ),
- array(
- 'pubdate:2007-01-01/2007-02-01 date:2008-01-01/2008-02-01',
- array(
- 'min_date' => '1199163600',
- 'max_date' => '1201928399',
- 'min_pubdate' => '1167627600',
- 'max_pubdate' => '1170392399',
- ),
- ),
- array(
- 'inurl:word1 author:word2 intitle:word3 pubdate:2007-01-01/2007-02-01 date:2008-01-01/2008-02-01 hello world',
- array(
- 'inurl' => 'word1',
- 'author' => 'word2',
- 'intitle' => 'word3',
- 'min_date' => '1199163600',
- 'max_date' => '1201928399',
- 'min_pubdate' => '1167627600',
- 'max_pubdate' => '1170392399',
- 'search' => 'hello world',
- ),
- ),
- );
- }
-
- /**
- * @dataProvider provideIntitleSearch
- * @param string $search
- * @param string $expected_value
- */
- public function testParseSearch_whenIntitleKeyword_returnArrayWithIntitleValue($search, $expected_value) {
- FreshRSS_Context::$search = $search;
- $parsed_search = $this->context->parseSearch();
- $this->assertEquals($expected_value, $parsed_search['intitle']);
- }
-
- /**
- * @return array
- */
- public function provideIntitleSearch() {
- return array(
- array('intitle:word1', 'word1'),
- array('intitle:word1 word2', 'word1'),
- array('intitle:"word1 word2"', 'word1 word2'),
- array("intitle:'word1 word2'", 'word1 word2'),
- array('word1 intitle:word2', 'word2'),
- array('word1 intitle:word2 word3', 'word2'),
- array('word1 intitle:"word2 word3"', 'word2 word3'),
- array("word1 intitle:'word2 word3'", 'word2 word3'),
- array('intitle:word1 intitle:word2', 'word1'),
- array('intitle: word1 word2', ''),
- array('intitle:123', '123'),
- array('intitle:"word1 word2" word3"', 'word1 word2'),
- array("intitle:'word1 word2' word3'", 'word1 word2'),
- array('intitle:"word1 word2\' word3"', "word1 word2' word3"),
- array("intitle:'word1 word2\" word3'", 'word1 word2" word3'),
- );
- }
-
- /**
- * @dataProvider provideAuthorSearch
- * @param string $search
- * @param string $expected_value
- */
- public function testParseSearch_whenAuthorKeyword_returnArrayWithAuthorValue($search, $expected_value) {
- FreshRSS_Context::$search = $search;
- $parsed_search = $this->context->parseSearch();
- $this->assertEquals($expected_value, $parsed_search['author']);
- }
-
- /**
- * @return array
- */
- public function provideAuthorSearch() {
- return array(
- array('author:word1', 'word1'),
- array('author:word1 word2', 'word1'),
- array('author:"word1 word2"', 'word1 word2'),
- array("author:'word1 word2'", 'word1 word2'),
- array('word1 author:word2', 'word2'),
- array('word1 author:word2 word3', 'word2'),
- array('word1 author:"word2 word3"', 'word2 word3'),
- array("word1 author:'word2 word3'", 'word2 word3'),
- array('author:word1 author:word2', 'word1'),
- array('author: word1 word2', ''),
- array('author:123', '123'),
- array('author:"word1 word2" word3"', 'word1 word2'),
- array("author:'word1 word2' word3'", 'word1 word2'),
- array('author:"word1 word2\' word3"', "word1 word2' word3"),
- array("author:'word1 word2\" word3'", 'word1 word2" word3'),
- );
- }
-
- /**
- * @dataProvider provideInurlSearch
- * @param string $search
- * @param string $expected_value
- */
- public function testParseSearch_whenInurlKeyword_returnArrayWithInurlValue($search, $expected_value) {
- FreshRSS_Context::$search = $search;
- $parsed_search = $this->context->parseSearch();
- $this->assertEquals($expected_value, $parsed_search['inurl']);
- }
-
- /**
- * @return array
- */
- public function provideInurlSearch() {
- return array(
- array('inurl:word1', 'word1'),
- array('inurl: word1', ''),
- array('inurl:123', '123'),
- array('inurl:word1 word2', 'word1'),
- array('inurl:"word1 word2"', '"word1'),
- );
- }
-
- /**
- * @dataProvider provideDateSearch
- * @param string $search
- * @param string $expected_min_value
- * @param string $expected_max_value
- */
- public function testParseSearch_whenDateKeyword_returnArrayWithDateValues($search, $expected_min_value, $expected_max_value) {
- FreshRSS_Context::$search = $search;
- $parsed_search = $this->context->parseSearch();
- $this->assertEquals($expected_min_value, $parsed_search['min_date']);
- $this->assertEquals($expected_max_value, $parsed_search['max_date']);
- }
-
- /**
- * @return array
- */
- public function provideDateSearch() {
- return array(
- array('date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'),
- array('date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'),
- array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'),
- array('date:2007-03-01/2008-05-11', '1172725200', '1210564799'),
- array('date:2007-03-01/', '1172725200', ''),
- array('date:/2008-05-11', '', '1210564799'),
- );
- }
-
- /**
- * @dataProvider providePubdateSearch
- * @param string $search
- * @param string $expected_min_value
- * @param string $expected_max_value
- */
- public function testParseSearch_whenPubdateKeyword_returnArrayWithPubdateValues($search, $expected_min_value, $expected_max_value) {
- FreshRSS_Context::$search = $search;
- $parsed_search = $this->context->parseSearch();
- $this->assertEquals($expected_min_value, $parsed_search['min_pubdate']);
- $this->assertEquals($expected_max_value, $parsed_search['max_pubdate']);
- }
-
- /**
- * @return array
- */
- public function providePubdateSearch() {
- return array(
- array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'),
- array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'),
- array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'),
- array('pubdate:2007-03-01/2008-05-11', '1172725200', '1210564799'),
- array('pubdate:2007-03-01/', '1172725200', ''),
- array('pubdate:/2008-05-11', '', '1210564799'),
- );
- }
-
}
diff --git a/tests/app/Models/SearchTest.php b/tests/app/Models/SearchTest.php
new file mode 100644
index 000000000..20ea09433
--- /dev/null
+++ b/tests/app/Models/SearchTest.php
@@ -0,0 +1,277 @@
+<?php
+
+require_once(LIB_PATH . '/lib_date.php');
+
+class SearchTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @dataProvider provideEmptyInput
+ * @param string|null $input
+ */
+ public function test__construct_whenInputIsEmpty_getsOnlyNullValues($input) {
+ $search = new FreshRSS_Search($input);
+ $this->assertNull($search->getRawInput());
+ $this->assertNull($search->getIntitle());
+ $this->assertNull($search->getMinDate());
+ $this->assertNull($search->getMaxDate());
+ $this->assertNull($search->getMinPubdate());
+ $this->assertNull($search->getMaxPubdate());
+ $this->assertNull($search->getAuthor());
+ $this->assertNull($search->getTags());
+ $this->assertNull($search->getSearch());
+ }
+
+ /**
+ * Return an array of values for the search object.
+ * Here is the description of the values
+ * @return array
+ */
+ public function provideEmptyInput() {
+ return array(
+ array(''),
+ array(null),
+ );
+ }
+
+ /**
+ * @dataProvider provideIntitleSearch
+ * @param string $input
+ * @param string $intitle_value
+ * @param string|null $search_value
+ */
+ public function test__construct_whenInputContainsIntitle_setsIntitlePropery($input, $intitle_value, $search_value) {
+ $search = new FreshRSS_Search($input);
+ $this->assertEquals($intitle_value, $search->getIntitle());
+ $this->assertEquals($search_value, $search->getSearch());
+ }
+
+ /**
+ * @return array
+ */
+ public function provideIntitleSearch() {
+ return array(
+ array('intitle:word1', 'word1', null),
+ array('intitle:word1 word2', 'word1', 'word2'),
+ array('intitle:"word1 word2"', 'word1 word2', null),
+ array("intitle:'word1 word2'", 'word1 word2', null),
+ array('word1 intitle:word2', 'word2', 'word1'),
+ array('word1 intitle:word2 word3', 'word2', 'word1 word3'),
+ array('word1 intitle:"word2 word3"', 'word2 word3', 'word1'),
+ array("word1 intitle:'word2 word3'", 'word2 word3', 'word1'),
+ array('intitle:word1 intitle:word2', 'word1', 'intitle:word2'),
+ array('intitle: word1 word2', null, 'word1 word2'),
+ array('intitle:123', '123', null),
+ array('intitle:"word1 word2" word3"', 'word1 word2', 'word3"'),
+ array("intitle:'word1 word2' word3'", 'word1 word2', "word3'"),
+ array('intitle:"word1 word2\' word3"', "word1 word2' word3", null),
+ array("intitle:'word1 word2\" word3'", 'word1 word2" word3', null),
+ );
+ }
+
+ /**
+ * @dataProvider provideAuthorSearch
+ * @param string $input
+ * @param string $author_value
+ * @param string|null $search_value
+ */
+ public function test__construct_whenInputContainsAuthor_setsAuthorValue($input, $author_value, $search_value) {
+ $search = new FreshRSS_Search($input);
+ $this->assertEquals($author_value, $search->getAuthor());
+ $this->assertEquals($search_value, $search->getSearch());
+ }
+
+ /**
+ * @return array
+ */
+ public function provideAuthorSearch() {
+ return array(
+ array('author:word1', 'word1', null),
+ array('author:word1 word2', 'word1', 'word2'),
+ array('author:"word1 word2"', 'word1 word2', null),
+ array("author:'word1 word2'", 'word1 word2', null),
+ array('word1 author:word2', 'word2', 'word1'),
+ array('word1 author:word2 word3', 'word2', 'word1 word3'),
+ array('word1 author:"word2 word3"', 'word2 word3', 'word1'),
+ array("word1 author:'word2 word3'", 'word2 word3', 'word1'),
+ array('author:word1 author:word2', 'word1', 'author:word2'),
+ array('author: word1 word2', null, 'word1 word2'),
+ array('author:123', '123', null),
+ array('author:"word1 word2" word3"', 'word1 word2', 'word3"'),
+ array("author:'word1 word2' word3'", 'word1 word2', "word3'"),
+ array('author:"word1 word2\' word3"', "word1 word2' word3", null),
+ array("author:'word1 word2\" word3'", 'word1 word2" word3', null),
+ );
+ }
+
+ /**
+ * @dataProvider provideInurlSearch
+ * @param string $input
+ * @param string $inurl_value
+ * @param string|null $search_value
+ */
+ public function test__construct_whenInputContainsInurl_setsInurlValue($input, $inurl_value, $search_value) {
+ $search = new FreshRSS_Search($input);
+ $this->assertEquals($inurl_value, $search->getInurl());
+ $this->assertEquals($search_value, $search->getSearch());
+ }
+
+ /**
+ * @return array
+ */
+ public function provideInurlSearch() {
+ return array(
+ array('inurl:word1', 'word1', null),
+ array('inurl: word1', null, 'word1'),
+ array('inurl:123', '123', null),
+ array('inurl:word1 word2', 'word1', 'word2'),
+ array('inurl:"word1 word2"', '"word1', 'word2"'),
+ );
+ }
+
+ /**
+ * @dataProvider provideDateSearch
+ * @param string $input
+ * @param string $min_date_value
+ * @param string $max_date_value
+ */
+ public function test__construct_whenInputContainsDate_setsDateValues($input, $min_date_value, $max_date_value) {
+ $search = new FreshRSS_Search($input);
+ $this->assertEquals($min_date_value, $search->getMinDate());
+ $this->assertEquals($max_date_value, $search->getMaxDate());
+ }
+
+ /**
+ * @return array
+ */
+ public function provideDateSearch() {
+ return array(
+ array('date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'),
+ array('date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'),
+ array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'),
+ array('date:2007-03-01/2008-05-11', '1172725200', '1210564799'),
+ array('date:2007-03-01/', '1172725200', ''),
+ array('date:/2008-05-11', '', '1210564799'),
+ );
+ }
+
+ /**
+ * @dataProvider providePubdateSearch
+ * @param string $input
+ * @param string $min_pubdate_value
+ * @param string $max_pubdate_value
+ */
+ public function test__construct_whenInputContainsPubdate_setsPubdateValues($input, $min_pubdate_value, $max_pubdate_value) {
+ $search = new FreshRSS_Search($input);
+ $this->assertEquals($min_pubdate_value, $search->getMinPubdate());
+ $this->assertEquals($max_pubdate_value, $search->getMaxPubdate());
+ }
+
+ /**
+ * @return array
+ */
+ public function providePubdateSearch() {
+ return array(
+ array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'),
+ array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'),
+ array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'),
+ array('pubdate:2007-03-01/2008-05-11', '1172725200', '1210564799'),
+ array('pubdate:2007-03-01/', '1172725200', ''),
+ array('pubdate:/2008-05-11', '', '1210564799'),
+ );
+ }
+
+ /**
+ * @dataProvider provideTagsSearch
+ * @param string $input
+ * @param string $tags_value
+ * @param string|null $search_value
+ */
+ public function test__construct_whenInputContainsTags_setsTagsValue($input, $tags_value, $search_value) {
+ $search = new FreshRSS_Search($input);
+ $this->assertEquals($tags_value, $search->getTags());
+ $this->assertEquals($search_value, $search->getSearch());
+ }
+
+ /**
+ * @return array
+ */
+ public function provideTagsSearch() {
+ return array(
+ array('#word1', array('word1'), null),
+ array('# word1', null, '# word1'),
+ array('#123', array('123'), null),
+ array('#word1 word2', array('word1'), 'word2'),
+ array('#"word1 word2"', array('"word1'), 'word2"'),
+ array('#word1 #word2', array('word1', 'word2'), null),
+ );
+ }
+
+ /**
+ * @dataProvider provideMultipleSearch
+ * @param string $input
+ * @param string $author_value
+ * @param string $min_date_value
+ * @param string $max_date_value
+ * @param string $intitle_value
+ * @param string $inurl_value
+ * @param string $min_pubdate_value
+ * @param string $max_pubdate_value
+ * @param array $tags_value
+ * @param string|null $search_value
+ */
+ public function test__construct_whenInputContainsMultipleKeywords_setsValues($input, $author_value, $min_date_value, $max_date_value, $intitle_value, $inurl_value, $min_pubdate_value, $max_pubdate_value, $tags_value, $search_value) {
+ $search = new FreshRSS_Search($input);
+ $this->assertEquals($author_value, $search->getAuthor());
+ $this->assertEquals($min_date_value, $search->getMinDate());
+ $this->assertEquals($max_date_value, $search->getMaxDate());
+ $this->assertEquals($intitle_value, $search->getIntitle());
+ $this->assertEquals($inurl_value, $search->getInurl());
+ $this->assertEquals($min_pubdate_value, $search->getMinPubdate());
+ $this->assertEquals($max_pubdate_value, $search->getMaxPubdate());
+ $this->assertEquals($tags_value, $search->getTags());
+ $this->assertEquals($search_value, $search->getSearch());
+ $this->assertEquals($input, $search->getRawInput());
+ }
+
+ public function provideMultipleSearch() {
+ return array(
+ array(
+ 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
+ 'word1',
+ '1172725200',
+ '1210564799',
+ 'word2',
+ 'word3',
+ '1172725200',
+ '1210564799',
+ array('word4', 'word5'),
+ null,
+ ),
+ array(
+ 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
+ 'word1',
+ '1172725200',
+ '1210564799',
+ 'word2',
+ 'word3',
+ '1172725200',
+ '1210564799',
+ array('word4', 'word5'),
+ 'word6',
+ ),
+ array(
+ 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
+ 'word1',
+ '1172725200',
+ '1210564799',
+ 'word2',
+ 'word3',
+ '1172725200',
+ '1210564799',
+ array('word4', 'word5'),
+ 'word6 word7',
+ ),
+ );
+ }
+
+}