(bug 37755) Set robot meta tags for 'view source' pages
[lhc/web/wiklou.git] / tests / phpunit / includes / db / ORMRowTest.php
1 <?php
2
3 /**
4 * Abstract class to construct tests for ORMRow deriving classes.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @since 1.20
23 *
24 * @ingroup Test
25 *
26 * @group ORM
27 *
28 * The database group has as a side effect that temporal database tables are created. This makes
29 * it possible to test without poisoning a production database.
30 * @group Database
31 *
32 * Some of the tests takes more time, and needs therefor longer time before they can be aborted
33 * as non-functional. The reason why tests are aborted is assumed to be set up of temporal databases
34 * that hold the first tests in a pending state awaiting access to the database.
35 * @group medium
36 *
37 * @licence GNU GPL v2+
38 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
39 */
40 abstract class ORMRowTest extends \MediaWikiTestCase {
41
42 /**
43 * @since 1.20
44 * @return string
45 */
46 protected abstract function getRowClass();
47
48 /**
49 * @since 1.20
50 * @return IORMTable
51 */
52 protected abstract function getTableInstance();
53
54 /**
55 * @since 1.20
56 * @return array
57 */
58 public abstract function constructorTestProvider();
59
60 /**
61 * @since 1.20
62 * @param IORMRow $row
63 * @param array $data
64 */
65 protected function verifyFields( IORMRow $row, array $data ) {
66 foreach ( array_keys( $data ) as $fieldName ) {
67 $this->assertEquals( $data[$fieldName], $row->getField( $fieldName ) );
68 }
69 }
70
71 /**
72 * @since 1.20
73 * @param array $data
74 * @param boolean $loadDefaults
75 * @return IORMRow
76 */
77 protected function getRowInstance( array $data, $loadDefaults ) {
78 $class = $this->getRowClass();
79 return new $class( $this->getTableInstance(), $data, $loadDefaults );
80 }
81
82 /**
83 * @since 1.20
84 * @return array
85 */
86 protected function getMockValues() {
87 return array(
88 'id' => 1,
89 'str' => 'foobar4645645',
90 'int' => 42,
91 'float' => 4.2,
92 'bool' => true,
93 'array' => array( 42, 'foobar' ),
94 'blob' => new stdClass()
95 );
96 }
97
98 /**
99 * @since 1.20
100 * @return array
101 */
102 protected function getMockFields() {
103 $mockValues = $this->getMockValues();
104 $mockFields = array();
105
106 foreach ( $this->getTableInstance()->getFields() as $name => $type ) {
107 if ( $name !== 'id' ) {
108 $mockFields[$name] = $mockValues[$type];
109 }
110 }
111
112 return $mockFields;
113 }
114
115 /**
116 * @since 1.20
117 * @return array of IORMRow
118 */
119 public function instanceProvider() {
120 $instances = array();
121
122 foreach ( $this->constructorTestProvider() as $arguments ) {
123 $instances[] = array( call_user_func_array( array( $this, 'getRowInstance' ), $arguments ) );
124 }
125
126 return $instances;
127 }
128
129 /**
130 * @dataProvider constructorTestProvider
131 */
132 public function testConstructor( array $data, $loadDefaults ) {
133 $this->verifyFields( $this->getRowInstance( $data, $loadDefaults ), $data );
134 }
135
136 /**
137 * @dataProvider constructorTestProvider
138 */
139 public function testSave( array $data, $loadDefaults ) {
140 $item = $this->getRowInstance( $data, $loadDefaults );
141
142 $this->assertTrue( $item->save() );
143
144 $this->assertTrue( $item->hasIdField() );
145 $this->assertTrue( is_integer( $item->getId() ) );
146
147 $id = $item->getId();
148
149 $this->assertTrue( $item->save() );
150
151 $this->assertEquals( $id, $item->getId() );
152
153 $this->verifyFields( $item, $data );
154 }
155
156 /**
157 * @dataProvider constructorTestProvider
158 */
159 public function testRemove( array $data, $loadDefaults ) {
160 $item = $this->getRowInstance( $data, $loadDefaults );
161
162 $this->assertTrue( $item->save() );
163
164 $this->assertTrue( $item->remove() );
165
166 $this->assertFalse( $item->hasIdField() );
167
168 $this->assertTrue( $item->save() );
169
170 $this->verifyFields( $item, $data );
171
172 $this->assertTrue( $item->remove() );
173
174 $this->assertFalse( $item->hasIdField() );
175
176 $this->verifyFields( $item, $data );
177 }
178
179 /**
180 * @dataProvider instanceProvider
181 */
182 public function testSetField( IORMRow $item ) {
183 foreach ( $this->getMockFields() as $name => $value ) {
184 $item->setField( $name, $value );
185 $this->assertEquals( $value, $item->getField( $name ) );
186 }
187 }
188
189 /**
190 * @since 1.20
191 * @param array $expected
192 * @param IORMRow $item
193 */
194 protected function assertFieldValues( array $expected, IORMRow $item ) {
195 foreach ( $expected as $name => $type ) {
196 if ( $name !== 'id' ) {
197 $this->assertEquals( $expected[$name], $item->getField( $name ) );
198 }
199 }
200 }
201
202 /**
203 * @dataProvider instanceProvider
204 */
205 public function testSetFields( IORMRow $item ) {
206 $originalValues = $item->getFields();
207
208 $item->setFields( array(), false );
209
210 foreach ( $item->getTable()->getFields() as $name => $type ) {
211 $originalHas = array_key_exists( $name, $originalValues );
212 $newHas = $item->hasField( $name );
213
214 $this->assertEquals( $originalHas, $newHas );
215
216 if ( $originalHas && $newHas ) {
217 $this->assertEquals( $originalValues[$name], $item->getField( $name ) );
218 }
219 }
220
221 $mockFields = $this->getMockFields();
222
223 $item->setFields( $mockFields, false );
224
225 $this->assertFieldValues( $originalValues, $item );
226
227 $item->setFields( $mockFields, true );
228
229 $this->assertFieldValues( $mockFields, $item );
230 }
231
232 // TODO: test all of the methods!
233
234 }