Passed
Push — develop ( 96d91c...861a09 )
by Schlaefer
04:14
created

testSearchSimpleNoAccession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace SaitoSearch\Test\Controller;
14
15
use Cake\ORM\TableRegistry;
16
use DateTimeImmutable;
17
use Saito\Exception\SaitoForbiddenException;
18
use Saito\Test\IntegrationTestCase;
19
20
/**
21
 * SearchesController Test Case
22
 *
23
 */
24
class SearchesControllerTest extends IntegrationTestCase
25
{
26
27
    /** @var array Fixtures */
28
    public $fixtures = [
29
        'app.Category',
30
        'app.Entry',
31
        'app.Setting',
32
        'app.User',
33
        'app.UserBlock',
34
        'app.UserIgnore',
35
        'app.UserRead',
36
        'app.UserOnline',
37
    ];
38
39
    /**
40
     * Sorting search results by rank
41
     */
42
    public function testSearchSimpleSortByRank()
43
    {
44
        $this->skipOnDataSource('Postgres');
45
        $this->_loginUser(1);
46
47
        $this->get('/searches/simple?searchTerm="Second_Subject"&order=rank');
48
49
        $this->assertResponseCode(200);
50
51
        $result = $this->viewVariable('results');
52
        $this->assertEquals(2, $result->first()->get('id'));
53
        $this->assertEquals(5, $result->skip(1)->first()->get('id'));
54
    }
55
56
    /**
57
     * Admin Category results should be in search results for admin
58
     */
59
    public function testSearchSimpleAccession()
60
    {
61
        $this->skipOnDataSource('Postgres');
62
        $this->_loginUser(1);
63
64
        $this->get('/searches/simple?searchTerm="Third+Thread+First_Subject"');
65
        $result = $this->viewVariable('results');
66
67
        $this->assertCount(1, $result);
68
    }
69
70
    /**
71
     * Admin Category results shouldn't be in search results for user
72
     */
73
    public function testSearchSimpleNoAccession()
74
    {
75
        $this->_loginUser(3);
76
77
        $this->get('/searches/simple?searchTerm="Third+Thread+First_Subject"');
78
        $result = $this->viewVariable('results');
79
80
        $this->assertCount(0, $result);
81
    }
82
83
    /**
84
     * Admin Category results should be in search results for admin
85
     */
86
    public function testSearchAdvancedAccession()
87
    {
88
        $url = '/searches/advanced?subject=Third+Thread+First_Subject&year[year]=1999';
89
90
        /// No access for normal user
91
        $this->_loginUser(3);
92
        $this->get($url);
93
        $result = $this->viewVariable('results');
94
        $this->assertCount(0, $result);
95
96
        /// Access for admin
97
        $this->_loginUser(1);
98
        $this->get($url);
99
        $result = $this->viewVariable('results');
100
        $this->assertCount(1, $result);
101
    }
102
103
    public function testAdvancedSearchWithNoExistingPostings()
104
    {
105
        $this->_loginUser(3);
106
107
        $EntriesTable = TableRegistry::getTableLocator()->get('Entries');
108
        $EntriesTable->deleteAll('id > 0');
109
110
        $url = '/searches/advanced?subject=foo';
111
        $this->get($url);
112
113
        $this->assertResponseCode(200);
114
        $result = $this->viewVariable('results');
115
        $this->assertCount(0, $result);
116
    }
117
118
    public function testSearchAdvancedCategoryNoAccession()
119
    {
120
        $this->_loginUser(3);
121
122
        $this->expectException(SaitoForbiddenException::class);
123
        $this->get('/searches/advanced?subject=Third+Thread+First_Subject&category_id=1');
124
    }
125
126
    public function testSearchAdvancedUserPostings()
127
    {
128
        $this->_loginUser(1);
129
130
        $this->get('/searches/advanced?name=Alice&year[year]=1999');
131
132
        $results = $this->viewVariable('results');
133
        $this->assertNotEmpty($results);
134
    }
135
136
    /**
137
     * Limit default search range to the last year
138
     */
139
    public function testSearchAdvancedSinceLastYear()
140
    {
141
        $this->_loginUser(3);
142
143
        $this->get('/searches/advanced');
144
145
        $actualMonth = $this->viewVariable('month');
146
        $expectedMonth = (new DateTimeImmutable())->format('n');
147
        $this->assertEquals($expectedMonth, $actualMonth);
148
    }
149
}
150