Completed
Pull Request — master (#274)
by Markus
06:46
created

OpdsTest::testPageIndex()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * COPS (Calibre OPDS PHP Server) test file
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     Sébastien Lucas <[email protected]>
7
 */
8
9
require_once (dirname(__FILE__) . "/config_test.php");
10
11
define ("OPDS_RELAX_NG", dirname(__FILE__) . "/opds-relax-ng/opds_catalog_1_1.rng");
12
define ("OPENSEARCHDESCRIPTION_RELAX_NG", dirname(__FILE__) . "/opds-relax-ng/opensearchdescription.rng");
13
define ("JING_JAR", dirname(__FILE__) . "/jing.jar");
14
define ("OPDSVALIDATOR_JAR", dirname(__FILE__) . "/OPDSValidator.jar");
15
define ("TEST_FEED", dirname(__FILE__) . "/text.atom");
16
17
class OpdsTest extends PHPUnit_Framework_TestCase
18
{
19
    public static function tearDownAfterClass()
20
    {
21
        if (!file_exists (TEST_FEED)) {
22
            return;
23
        }
24
        unlink (TEST_FEED);
25
    }
26
27
    function jingValidateSchema($feed, $relax = OPDS_RELAX_NG) {
28
        $path = "";
29
        $res = system($path . 'java -jar "' . JING_JAR . '" "' . $relax . '" "' . $feed . '"');
30
        if ($res != '') {
31
            echo 'RelaxNG validation error: '.$res;
32
            return false;
33
        } else
34
            return true;
35
    }
36
37
    function opdsValidator($feed) {
38
        $oldcwd = getcwd(); // Save the old working directory
39
        chdir("test");
40
        $path = "";
41
        $res = system($path . 'java -jar "' . OPDSVALIDATOR_JAR . '" "' . $feed . '"');
42
        chdir($oldcwd);
43
        if ($res != '') {
44
            echo 'OPDS validation error: '.$res;
45
            return false;
46
        } else
47
            return true;
48
    }
49
50
    function opdsCompleteValidation ($feed) {
51
        return $this->jingValidateSchema($feed) && $this->opdsValidator($feed);
52
    }
53
54
    public function testPageIndex ()
55
    {
56
        global $config;
57
        $page = Base::PAGE_INDEX;
58
        $query = NULL;
59
        $qid = NULL;
60
        $n = "1";
61
62
        $_SERVER['QUERY_STRING'] = "";
63
        $config['cops_subtitle_default'] = "My subtitle";
64
65
        $currentPage = Page::getPage ($page, $qid, $query, $n);
66
        $currentPage->InitializeContent ();
67
68
        $OPDSRender = new OPDSRenderer ();
69
70
        file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
71
        $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
72
73
        $_SERVER ["HTTP_USER_AGENT"] = "XXX";
74
        $config['cops_generate_invalid_opds_stream'] = "1";
75
76
        file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
77
        $this->AssertFalse ($this->jingValidateSchema (TEST_FEED));
78
        $this->AssertFalse ($this->opdsValidator (TEST_FEED));
79
80
        $_SERVER['QUERY_STRING'] = NULL;
81
    }
82
83
    /**
84
     * @dataProvider providerPage
85
     */
86
    public function testMostPages ($page, $query)
87
    {
88
        $qid = NULL;
89
        $n = "1";
90
        $_SERVER['QUERY_STRING'] = "?page={$page}";
91
        if (!empty ($query)) {
92
            $_SERVER['QUERY_STRING'] .= "&query={$query}";
93
        }
94
        $_SERVER['REQUEST_URI'] = "feed.php" . $_SERVER['QUERY_STRING'];
95
96
        $currentPage = Page::getPage ($page, $qid, $query, $n);
97
        $currentPage->InitializeContent ();
98
99
        $OPDSRender = new OPDSRenderer ();
100
101
        file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
102
        $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
103
    }
104
105
    public function providerPage ()
106
    {
107
        return array (
108
            array (Base::PAGE_OPENSEARCH, "car"),
109
            array (Base::PAGE_ALL_AUTHORS, NULL),
110
            array (Base::PAGE_ALL_SERIES, NULL),
111
            array (Base::PAGE_ALL_TAGS, NULL),
112
            array (Base::PAGE_ALL_PUBLISHERS, NULL),
113
            array (Base::PAGE_ALL_LANGUAGES, NULL),
114
            array (Base::PAGE_ALL_RECENT_BOOKS, NULL),
115
            array (Base::PAGE_ALL_BOOKS, NULL)
116
        );
117
    }
118
119
    public function testPageIndexMultipleDatabase ()
120
    {
121
        global $config;
122
        $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
123
                                              "One book" => dirname(__FILE__) . "/BaseWithOneBook/");
124
        $page = Base::PAGE_INDEX;
125
        $query = NULL;
126
        $qid = "1";
127
        $n = "1";
128
        $_SERVER['QUERY_STRING'] = "";
129
130
        $currentPage = Page::getPage ($page, $qid, $query, $n);
131
        $currentPage->InitializeContent ();
132
133
        $OPDSRender = new OPDSRenderer ();
134
135
        file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
136
        $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
137
    }
138
139
    public function testOpenSearchDescription ()
140
    {
141
        $_SERVER['QUERY_STRING'] = "";
142
143
        $OPDSRender = new OPDSRenderer ();
144
145
        file_put_contents (TEST_FEED, $OPDSRender->getOpenSearch ());
146
        $this->AssertTrue ($this->jingValidateSchema (TEST_FEED, OPENSEARCHDESCRIPTION_RELAX_NG));
147
148
        $_SERVER['QUERY_STRING'] = NULL;
149
    }
150
151
    public function testPageAuthorMultipleDatabase ()
152
    {
153
        global $config;
154
        $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
155
                                              "One book" => dirname(__FILE__) . "/BaseWithOneBook/");
156
        $page = Base::PAGE_AUTHOR_DETAIL;
157
        $query = NULL;
158
        $qid = "1";
159
        $n = "1";
160
        $_SERVER['QUERY_STRING'] = "page=" . Base::PAGE_AUTHOR_DETAIL . "&id=1";
161
        $_GET ["db"] = "0";
162
163
        $currentPage = Page::getPage ($page, $qid, $query, $n);
164
        $currentPage->InitializeContent ();
165
166
        $OPDSRender = new OPDSRenderer ();
167
168
        file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
169
        $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
170
    }
171
172
    public function testPageAuthorsDetail ()
173
    {
174
        global $config;
175
        $page = Base::PAGE_AUTHOR_DETAIL;
176
        $query = NULL;
177
        $qid = "1";
178
        $n = "1";
179
        $_SERVER['QUERY_STRING'] = "page=" . Base::PAGE_AUTHOR_DETAIL . "&id=1&n=1";
180
181
        $config['cops_max_item_per_page'] = 2;
182
183
        // First page
184
185
        $currentPage = Page::getPage ($page, $qid, $query, $n);
186
        $currentPage->InitializeContent ();
187
188
        $OPDSRender = new OPDSRenderer ();
189
190
        file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
191
        $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
192
193
        // Second page
194
195
        $n = 2;
196
        $currentPage = Page::getPage ($page, $qid, $query, $n);
197
        $currentPage->InitializeContent ();
198
199
        $OPDSRender = new OPDSRenderer ();
200
201
        file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
202
        $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
203
204
        // No pagination
205
        $config['cops_max_item_per_page'] = -1;
206
207
    }
208
209
    public function testPageAuthorsDetail_WithFacets ()
210
    {
211
        global $config;
212
        $page = Base::PAGE_AUTHOR_DETAIL;
213
        $query = NULL;
214
        $qid = "1";
215
        $n = "1";
216
        $_SERVER['QUERY_STRING'] = "page=" . Base::PAGE_AUTHOR_DETAIL . "&id=1&n=1";
217
        $_GET["tag"] = "Short Stories";
218
219
        $config['cops_books_filter'] = array ("Only Short Stories" => "Short Stories", "No Short Stories" => "!Short Stories");
220
221
        $currentPage = Page::getPage ($page, $qid, $query, $n);
222
        $currentPage->InitializeContent ();
223
224
        $OPDSRender = new OPDSRenderer ();
225
226
        file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
227
        $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
228
229
        $config['cops_books_filter'] = array ();
230
    }
231
232
    public function testPageAuthorsDetail_WithoutAnyId ()
233
    {
234
        global $config;
235
        $page = Base::PAGE_AUTHOR_DETAIL;
236
        $query = NULL;
237
        $qid = "1";
238
        $n = "1";
239
        $_SERVER['QUERY_STRING'] = "page=" . Base::PAGE_AUTHOR_DETAIL . "&id=1&n=1";
240
        $_SERVER['REQUEST_URI'] = "index.php?XXXX";
241
242
243
        $currentPage = Page::getPage ($page, $qid, $query, $n);
244
        $currentPage->InitializeContent ();
245
        $currentPage->idPage = NULL;
246
247
        $OPDSRender = new OPDSRenderer ();
248
249
        file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
250
        $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
251
252
    }
253
}