From 3f084916e3befbaadf8dc86c8329a408dfcdf351 Mon Sep 17 00:00:00 2001
From: Frederic Guillot <fred@kanboard.net>
Date: Sun, 28 Jun 2015 21:53:50 -0400
Subject: Add category attribute for advanced search

---
 tests/units/LexerTest.php      | 25 +++++++++++++++++++++++
 tests/units/TaskFilterTest.php | 45 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

(limited to 'tests')

diff --git a/tests/units/LexerTest.php b/tests/units/LexerTest.php
index e2b97566..4d0d67ad 100644
--- a/tests/units/LexerTest.php
+++ b/tests/units/LexerTest.php
@@ -66,6 +66,31 @@ class LexerTest extends Base
         );
     }
 
+    public function testCategoryQuery()
+    {
+        $lexer = new Lexer;
+
+        $this->assertEquals(
+            array(array('match' => 'category:', 'token' => 'T_CATEGORY'), array('match' => 'Feature Request', 'token' => 'T_STRING')),
+            $lexer->tokenize('category:"Feature Request"')
+        );
+
+        $this->assertEquals(
+            array('T_CATEGORY' => array('Feature Request')),
+            $lexer->map($lexer->tokenize('category:"Feature Request"'))
+        );
+
+        $this->assertEquals(
+            array('T_CATEGORY' => array('Feature Request', 'Bug')),
+            $lexer->map($lexer->tokenize('category:"Feature Request" category:Bug'))
+        );
+
+        $this->assertEquals(
+            array(),
+            $lexer->map($lexer->tokenize('category: '))
+        );
+    }
+
     public function testStatusQuery()
     {
         $lexer = new Lexer;
diff --git a/tests/units/TaskFilterTest.php b/tests/units/TaskFilterTest.php
index 494a0f1b..cec30394 100644
--- a/tests/units/TaskFilterTest.php
+++ b/tests/units/TaskFilterTest.php
@@ -7,6 +7,7 @@ use Model\User;
 use Model\TaskFilter;
 use Model\TaskCreation;
 use Model\DateParser;
+use Model\Category;
 
 class TaskFilterTest extends Base
 {
@@ -74,6 +75,50 @@ class TaskFilterTest extends Base
         $this->assertEmpty($tasks);
     }
 
+    public function testSearchWithCategory()
+    {
+        $p = new Project($this->container);
+        $c = new Category($this->container);
+        $tc = new TaskCreation($this->container);
+        $tf = new TaskFilter($this->container);
+
+        $this->assertEquals(1, $p->create(array('name' => 'test')));
+        $this->assertEquals(1, $c->create(array('name' => 'Feature request', 'project_id' => 1)));
+        $this->assertEquals(2, $c->create(array('name' => 'hé hé', 'project_id' => 1)));
+        $this->assertNotFalse($tc->create(array('project_id' => 1, 'title' => 'task1')));
+        $this->assertNotFalse($tc->create(array('project_id' => 1, 'title' => 'task2', 'category_id' => 1)));
+        $this->assertNotFalse($tc->create(array('project_id' => 1, 'title' => 'task3', 'category_id' => 2)));
+
+        $tf->search('category:"Feature request"');
+        $tasks = $tf->findAll();
+        $this->assertNotEmpty($tasks);
+        $this->assertCount(1, $tasks);
+        $this->assertEquals('task2', $tasks[0]['title']);
+
+        $tf->search('category:"hé hé"');
+        $tasks = $tf->findAll();
+        $this->assertNotEmpty($tasks);
+        $this->assertCount(1, $tasks);
+        $this->assertEquals('task3', $tasks[0]['title']);
+
+        $tf->search('category:"Feature request" category:"hé hé"');
+        $tasks = $tf->findAll();
+        $this->assertNotEmpty($tasks);
+        $this->assertCount(2, $tasks);
+        $this->assertEquals('task2', $tasks[0]['title']);
+        $this->assertEquals('task3', $tasks[1]['title']);
+
+        $tf->search('category:none');
+        $tasks = $tf->findAll();
+        $this->assertNotEmpty($tasks);
+        $this->assertCount(1, $tasks);
+        $this->assertEquals('task1', $tasks[0]['title']);
+
+        $tf->search('category:"not found"');
+        $tasks = $tf->findAll();
+        $this->assertEmpty($tasks);
+    }
+
     public function testSearchWithDueDate()
     {
         $dp = new DateParser($this->container);
-- 
cgit v1.2.3