4 * Copyright (c) STMicroelectronics, 2006. All Rights Reserved.
6 * Originally written by Mohamed CHAARI, 2007.
8 * This file is a part of codendi.
10 * codendi is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * codendi is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 require_once(dirname(__FILE__).'/../include/ForumML_FileStorage.class.php');
29 Mock::generatePartial('ForumML_FileStorage', 'ForumML_FileStorageTestVersion', array('fileExists'));
31 class ForumML_FileStorageTest extends UnitTestCase {
33 private $_namePattern;
36 function __construct($name="ForumML Mail Attachments Storage Test") {
37 parent::__construct($name);
38 $this->_fixture = dirname(__FILE__).'/_fixtures';
39 // validchar for attachment name
40 $this->_namePattern = "`[^a-z0-9_-]`i";
43 private function _deleteIfExists($path) {
46 } elseif (file_exists($path)) {
51 private function _getFileStorage($path) {
52 $fs = new ForumML_FileStorageTestVersion($this);
54 $fs->setReturnValue('fileExists', false);
62 $this->_deleteIfExists($this->_fixture.'/gpig-interest/2007_10_24/Screenshot_jpg');
63 $this->_deleteIfExists($this->_fixture.'/gpig-interest/2007_10_24');
64 $this->_deleteIfExists($this->_fixture.'/gpig-interest');
68 function testForumML_FileStorage() {
69 $fstorage = $this->_getFileStorage($this->_fixture);
70 $this->assertNotNull($fstorage->root);
71 $this->assertIsA($fstorage->root, 'string');
72 $this->assertEqual($fstorage->root,$this->_fixture);
73 $this->assertNoErrors();
76 // case 1: an attachment file whose name has more than 64 characters
77 function test_getPathFileNameWithMoreThan64Char() {
78 $fs1 = $this->_getFileStorage($this->_fixture);
79 $name1 = "a string with more than 64 characters, which is the limit allowed for ForumML attachments";
80 $list1 = "gpig-interest";
81 $date1 = "2007_10_24";
84 // check returned path
85 $path1 = $fs1->_getPath($name1,$list1,$date1,$type1);
86 $this->assertNotNull($path1);
87 $this->assertIsA($path1, 'string');
88 $this->assertNoErrors();
89 // check filename length is restricted to 64 characters
90 $path_array1 = explode("/",$path1);
91 $fname1 = $path_array1[count($path_array1) - 1];
92 $this->assertNotEqual($name1,$fname1);
93 $this->assertEqual(strlen($fname1),63);
94 // check other path components
95 $flist1 = $path_array1[count($path_array1) - 3];
96 $this->assertEqual($flist1,$list1);
97 $fdate1 = $path_array1[count($path_array1) - 2];
98 $this->assertEqual($fdate1,$date1);
100 $this->assertWantedPattern($this->_namePattern,$name1);
103 // case 2: an attachment file whose name has less than 64 characters
104 function test_getPathFileNameWithLessThan64Char() {
105 $fs1 = $this->_getFileStorage($this->_fixture);
106 $name2 = "filename less than 64 chars";
107 $list1 = "gpig-interest";
108 $date1 = "2007_10_24";
111 $path2 = $fs1->_getPath($name2,$list1,$date1,$type1);
112 $this->assertNotNull($path2);
113 $this->assertIsA($path2, 'string');
114 $this->assertNoErrors();
115 $path_array2 = explode("/",$path2);
116 $fname2 = $path_array2[count($path_array2) - 1];
117 $this->assertEqual($fname2,"filename_less_than_64_chars");
118 $this->assertNotEqual(strlen($fname2),64);
119 // check path components
120 $flist2 = $path_array2[count($path_array2) - 3];
121 $this->assertEqual($flist2,$list1);
122 $fdate2 = $path_array2[count($path_array2) - 2];
123 $this->assertEqual($fdate2,$date1);
125 $this->assertWantedPattern($this->_namePattern,$name2);
128 // case 3: attachment filename with only alphanumeric characters
129 function test_getPathFileNameWithAlphaNumCharsOnly() {
130 $fs1 = $this->_getFileStorage($this->_fixture);
131 $name3 = "Cx2008-requirements";
132 $list1 = "gpig-interest";
133 $date1 = "2007_10_24";
136 $path3 = $fs1->_getPath($name3,$list1,$date1,$type1);
137 $this->assertNotNull($path3);
138 $this->assertIsA($path3, 'string');
139 $this->assertNoErrors();
140 $path_array3 = explode("/",$path3);
141 $fname3 = $path_array3[count($path_array3) - 1];
142 $this->assertNoUnwantedPattern($this->_namePattern,$name3);
145 // case 4: attachment filename is an empty string
146 function test_getPathFileNameEmpty() {
147 $fs1 = $this->_getFileStorage($this->_fixture);
149 $list1 = "gpig-interest";
150 $date1 = "2007_10_24";
153 $path4 = $fs1->_getPath($name4,$list1,$date1,$type1);
154 $this->assertNoErrors();
155 $this->assertNotNull($path4);
156 $this->assertIsA($path4, 'string');
157 $path_array4 = explode("/",$path4);
158 $fname4 = $path_array4[count($path_array4) - 1];
159 $this->assertWantedPattern('/^attachment.*/', $fname4);
162 // case 5: same attachment name submitted 2 times same day for same list
163 function testGetPathWithSameFileName() {
164 $fs = new ForumML_FileStorageTestVersion($this);
165 $fs->root = $this->_fixture;
166 $fs->setReturnValueAt(0, 'fileExists', false);
167 $fs->setReturnValueAt(1, 'fileExists', true);
169 $list = "gpig-interest";
170 $date = "2007_10_24";
172 $name = 'Screenshot.jpg';
174 // First file stored that day
175 $path1 = $fs->_getPath($name,$list,$date,$type);
177 // Second file with same name
178 $path2 = $fs->_getPath($name,$list,$date,$type);
180 $this->assertNotEqual($path1, $path2);