aedc23ad8ea58636319e5f2ea80850ad9e58514d
[lhc/web/wiklou.git] / tests / phpunit / includes / media / DjVuTest.php
1 <?php
2 /**
3 * @covers DjVuHandler
4 */
5 class DjVuTest extends MediaWikiTestCase {
6
7 /**
8 * @var string the directory where test files are
9 */
10 protected $filePath;
11
12 /**
13 * @var FSRepo the repository to use
14 */
15 protected $repo;
16
17 /**
18 * @var DjVuHandler
19 */
20 protected $handler;
21
22 protected function setUp() {
23 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
24 parent::setUp();
25
26 //cli tool setup
27 $wgDjvuRenderer = $wgDjvuRenderer ? $wgDjvuRenderer : '/usr/bin/ddjvu';
28 $wgDjvuDump = $wgDjvuDump ? $wgDjvuDump : '/usr/bin/djvudump';
29 $wgDjvuToXML = $wgDjvuToXML ? $wgDjvuToXML : '/usr/bin/djvutoxml';
30 if (
31 !$this->checkIfToolExists( $wgDjvuRenderer ) ||
32 !$this->checkIfToolExists( $wgDjvuDump ) ||
33 !$this->checkIfToolExists( $wgDjvuToXML )
34 ) {
35 $this->markTestSkipped( 'This test needs the installation of the ddjvu, djvutoxml and djvudump tools' );
36 }
37
38 //file repo setup
39 $this->filePath = __DIR__ . '/../../data/media/';
40 $backend = new FSFileBackend( array(
41 'name' => 'localtesting',
42 'wikiId' => wfWikiId(),
43 'lockManager' => new NullLockManager( array() ),
44 'containerPaths' => array( 'data' => $this->filePath )
45 ) );
46 $this->repo = new FSRepo( array(
47 'name' => 'temp',
48 'url' => 'http://localhost/thumbtest',
49 'backend' => $backend
50 ) );
51
52 $this->handler = new DjVuHandler();
53 }
54
55 /**
56 * Check if a tool exist
57 *
58 * @param string $path path to the tool
59 * @return bool
60 */
61 protected function checkIfToolExists( $path ) {
62 wfSuppressWarnings();
63 $result = file_exists( $path );
64 wfRestoreWarnings();
65 return $result;
66 }
67
68 protected function dataFile( $name, $type ) {
69 return new UnregisteredLocalFile(
70 false,
71 $this->repo,
72 'mwstore://localtesting/data/' . $name,
73 $type
74 );
75 }
76
77 public function testGetImageSize() {
78 $this->assertArrayEquals(
79 array( 2480, 3508, 'DjVu', 'width="2480" height="3508"' ),
80 $this->handler->getImageSize( null, $this->filePath . '/LoremIpsum.djvu' ),
81 'Test file LoremIpsum.djvu should have a size of 2480 * 3508'
82 );
83 }
84
85 public function testInvalidFile() {
86 $this->assertFalse(
87 $this->handler->getMetadata( null, $this->filePath . '/README' ),
88 'Getting Metadata for an inexistent file should returns false'
89 );
90 }
91
92 public function testPageCount() {
93 $file = $this->dataFile( 'LoremIpsum.djvu', 'image/x.djvu' );
94 $this->assertEquals(
95 5,
96 $this->handler->pageCount( $file ),
97 'Test file LoremIpsum.djvu should be detected as containing 5 pages'
98 );
99 }
100
101 public function testGetPageDimensions() {
102 $file = $this->dataFile( 'LoremIpsum.djvu', 'image/x.djvu' );
103 $this->assertArrayEquals(
104 array( 2480, 3508 ),
105 $this->handler->getPageDimensions( $file, 1 ),
106 'Page 1 of test file LoremIpsum.djvu should have a size of 2480 * 3508'
107 );
108 }
109
110 public function testGetPageText() {
111 $file = $this->dataFile( 'LoremIpsum.djvu', 'image/x.djvu' );
112 $this->assertEquals(
113 "Lorem ipsum \n1 \n",
114 (string) $this->handler->getPageText( $file, 1 ),
115 "Text layer of page 1 of file LoremIpsum.djvu should be 'Lorem ipsum \n1 \n'"
116 );
117 }
118 }