Fixing #138
[akelos.git] / test / unit / lib / AkActiveRecord / _AkActiveRecord_1.php
blob56f209500b70f773c3f0769d2d54069cfa02b5c9
1 <?php
3 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true);
4 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php');
6 class test_AkActiveRecord extends AkUnitTest
9 function test_AkActiveRecord()
11 $this->installAndIncludeModels(array(
12 'AkTestUser'=>'id I AUTO KEY, user_name C(32), first_name C(200), last_name C(200), email C(150), country I, password C(32), created_at T, updated_at T, expires_on T',
13 'AkTestMember'=>'ak_test_user_id I, role C(25)',
14 'AkTestComment'=>'id I AUTO KEY, ak_test_user_id I, private_comment L, birth_date T',
15 'AkTestField'=>'id I AUTO KEY,varchar_field C(255),longtext_field XL,text_field X,logblob_field B,date_field D,
16 datetime_field T,tinyint_field I2,integer_field I,smallint_field I2,bigint_field I8,double_field F,
17 numeric_field N(10.5),bytea_field B,timestamp_field T,
18 boolean_field L,int2_field I2,int4_field I4,int8_field I8,foat_field F,varchar4000_field X,
19 clob_field XL,nvarchar2000_field X2,blob_field B,nvarchar_field C2(255),
20 decimal1_field L,'.//*decimal3_field I1,
21 'decimal5_field I2,decimal10_field I4,decimal20_field I8,decimal_field N(10.5),
22 created_at T,updated_at T,expires_on T'));
25 function Test_of_getArrayFromAkString()
27 $User = new AkTestUser();
28 $expected = array('name', 'last_name','options','date');
29 $this->assertEqual($User->getArrayFromAkString('name,last_name,options,date'),$expected);
30 $this->assertEqual($User->getArrayFromAkString(' name AND last_name ; options | date '),$expected);
31 $this->assertEqual($User->getArrayFromAkString(' name and last_name + options , date '),$expected);
33 /**/
34 function Test_of_parseAkelosArgs()
36 $User = new AkTestUser();
38 $expected = array('name'=>'Bermi', 'last_name'=>'Ferrer','options'=>array('admin'=>true,'expire'=>'never'),'date'=>'1978-06-16');
39 $akelos_args = array('name->','Bermi', 'last_name->','Ferrer','options'=>array('admin'=>true,'expire'=>'never'),'date->','1978-06-16');
40 $User->parseAkelosArgs($akelos_args);
41 $this->assertEqual($akelos_args,$expected);
42 $User->parseAkelosArgs($expected);
43 $this->assertEqual($akelos_args,$expected);
45 /**/
46 function Test_of_isConnected()
48 $User = new AkTestUser();
49 $User->setConnection();
50 $this->assertTrue($User->isConnected());
53 function Test_of_new_object_instantation()
55 $AkTestUser = new AkTestUser();
57 $this->assertEqual($AkTestUser->getModelName(), 'AkTestUser');
58 $this->assertEqual($AkTestUser->getTableName(), 'ak_test_users');
59 $this->assertErrorPattern('/ak_test_user/',$AkTestUser->setTableName('ak_test_user'));
61 //$this->_createNewTestingModel('AkTestUnavailableDatabase');
62 //$AkTestUnavailableDatabase = new AkTestUnavailableDatabase();
63 //$this->assertEqual($AkTestUnavailableDatabase->getModelName(), 'AkTestUnavailableDatabase');
64 //ak_define('AK_ACTIVE_RECORD_VALIDATE_TABLE_NAMES', true);
65 //$this->assertErrorPattern('/Ooops! Could not fetch details for the table ak_test_unavailable_database./',$AkTestUnavailableDatabase->getTableName());
69 // Test_of_setConnection(){} //This will not be tested due its simplicity
71 function Test_of_descendsFromActiveRecord()
73 $User = new AkTestUser();
74 $TestField = new AkTestField();
76 $this->assertTrue($User->descendsFromActiveRecord($TestField));
78 $Object = new AkObject();
79 $this->assertFalse($User->descendsFromActiveRecord($Object));
82 function Test_of_getModelName()
84 $AkTestUser = new AkTestUser();
85 $this->assertEqual($AkTestUser->getModelName(), 'AkTestUser');
87 $AkTestField = new AkTestField();
88 $this->assertEqual($AkTestField->getModelName(), 'AkTestField');
92 function Test_of_set_and_getParentModelName()
94 $AkTestMember = new AkTestMember();
95 $this->assertEqual($AkTestMember->getParentModelName(), 'AkTestUser');
97 $AkTestUser = new AkTestUser();
98 $this->assertErrorPattern('/YourParentModelName/',$AkTestUser->getParentModelName());
100 $AkTestUser = new AkTestUser();
101 $AkTestUser->setParentModelName('FakeClass');
102 $this->assertEqual($AkTestUser->getParentModelName(), 'FakeClass');
106 // More db type inspection on Test_of_db_inspection method of this test
107 function Test_of_getAkelosDataType()
109 $AkTestField = new AkTestField();
111 $adodb_column_object = new AkObject();
112 $adodb_column_object->name = 'decimal_field';
114 $adodb_column_object->type = 'BLOB';
115 $this->assertEqual('binary',$AkTestField->getAkelosDataType($adodb_column_object));
117 $adodb_column_object->type = 'integer';
118 $adodb_column_object->auto_increment = 1;
119 $this->assertEqual('serial',$AkTestField->getAkelosDataType($adodb_column_object));
121 $AkTestField->decimalFieldDataType = 'string';
122 $this->assertEqual('string',$AkTestField->getAkelosDataType($adodb_column_object));
125 // This test is implemented in Test_of_db_inspection
126 // function Test_of_setColumnSettings(){
127 // }
129 // More of this testing in Test_of_db_inspection
130 function Test_of_loadColumnsSettings()
132 $AkTestField = new AkTestField();
133 $AkTestField->loadColumnsSettings();
134 // Testing database settings cache on session (this might be changed in a future
135 $this->assertEqual($AkTestField->_columnsSettings,$_SESSION['__activeRecordColumnsSettingsCache']['AkTestField_column_settings']);
138 function Test_of_initiateColumnsToNull()
140 $AkTestField = new AkTestField();
141 $AkTestField->loadColumnsSettings();
143 $columns = array('id','varchar_field','longtext_field','text_field','logblob_field','date_field','datetime_field','tinyint_field','integer_field','smallint_field',
144 'bigint_field','double_field','numeric_field','bytea_field','timestamp_field','boolean_field','int2_field','int4_field',
145 'int8_field','foat_field','varchar4000_field','clob_field','nvarchar2000_field','blob_field','nvarchar_field','decimal1_field',//'decimal3_field',
146 'decimal5_field','decimal10_field','decimal20_field','decimal_field','created_at','updated_at','expires_on'
149 foreach ($columns as $column){
150 $this->assertFalse(isset($AkTestField->$column));
153 $AkTestField->initiateColumnsToNull();
155 foreach ($columns as $column){
156 $this->assertTrue(is_null($AkTestField->$column));
161 function Test_of_getColumnSettings()
163 $AkTestField = new AkTestField();
165 $columns = array('id','varchar_field','longtext_field','text_field','logblob_field','date_field','datetime_field','tinyint_field','integer_field','smallint_field',
166 'bigint_field','double_field','numeric_field','bytea_field','timestamp_field','boolean_field','int2_field','int4_field',
167 'int8_field','foat_field','varchar4000_field','clob_field','nvarchar2000_field','blob_field','nvarchar_field','decimal1_field',//'decimal3_field',
168 'decimal5_field','decimal10_field','decimal20_field','decimal_field','created_at','updated_at','expires_on'
171 $got = $AkTestField->getColumnSettings();
173 $this->assertTrue($got['id']['type'] == 'serial' && $got['id']['primaryKey'] == true);
174 foreach ($columns as $column){
175 $this->assertTrue(isset($got[$column]) && $got[$column]['name'] == $column && !empty($got[$column]['type']));
180 function Test_of_get_and_get_PrimaryKey()
182 $AkTestField = new AkTestField();
183 $this->assertEqual($AkTestField->getPrimaryKey(), 'id');
185 $AkTestField->setPrimaryKey('text_field');
186 $this->assertEqual($AkTestField->getPrimaryKey(), 'text_field');
188 $this->assertError($AkTestField->setPrimaryKey('unavailable_field'),'unavailable_field');
191 function Test_of_get_and_get_TableName()
193 $AkTestField = new AkTestField();
194 $this->assertEqual($AkTestField->getTableName(), 'ak_test_fields');
196 $this->assertError($AkTestField->setTableName('table_not_available_on_database'),'AK_ACTIVE_CONTROLLER_VALIDATE_TABLE_NAMES');
198 $this->assertTrue($AkTestField->setTableName('ak_test_users'));
199 $this->assertEqual($AkTestField->getTableName(), 'ak_test_users');
202 function Test_of_hasColumn()
204 $AkTestUser = new AkTestUser();
205 $this->assertTrue($AkTestUser->hasColumn('first_name'));
206 $this->assertFalse($AkTestUser->hasColumn('not_valid'));
207 $AkTestUser->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
208 $this->assertFalse($AkTestUser->hasColumn('name'));
211 function Test_of_getColumnNames()
213 $AkTestField = new AkTestField();
214 $expected = array ( 'id' => 'Id', 'varchar_field' => 'Varchar field', 'longtext_field' => 'Longtext field', 'text_field' => 'Text field', 'logblob_field' => 'Logblob field', 'date_field' => 'Date field', 'datetime_field' => 'Datetime field', 'tinyint_field' => 'Tinyint field', 'integer_field' => 'Integer field', 'smallint_field' => 'Smallint field', 'bigint_field' => 'Bigint field', 'double_field' => 'Double field', 'numeric_field' => 'Numeric field', 'bytea_field' => 'Bytea field', 'timestamp_field' => 'Timestamp field', 'boolean_field' => 'Boolean field', 'int2_field' => 'Int2 field', 'int4_field' => 'Int4 field', 'int8_field' => 'Int8 field', 'foat_field' => 'Foat field', 'varchar4000_field' => 'Varchar4000 field', 'clob_field' => 'Clob field', 'nvarchar2000_field' => 'Nvarchar2000 field', 'blob_field' => 'Blob field', 'nvarchar_field' => 'Nvarchar field', 'decimal1_field' => 'Decimal1 field', /*'decimal3_field' => 'Decimal3 field',*/ 'decimal5_field' => 'Decimal5 field', 'decimal10_field' => 'Decimal10 field', 'decimal20_field' => 'Decimal20 field', 'decimal_field' => 'Decimal field', 'created_at' => 'Created at', 'updated_at' => 'Updated at', 'expires_on' => 'Expires on' );
215 $this->assertEqual($AkTestField->getColumnNames(), $expected);
218 // This is tested in db inspection tests (Test_of_db_inspection)
219 // function Test_of_getColumns(){}
222 function Test_of_db_inspection()
224 $AkTestUser = new AkTestUser();
226 $expected = array(
227 'id' => array ('name' => 'id', 'type' => 'serial', 'primaryKey' => true),
228 'user_name' => array ( 'name' => 'user_name', 'type' => 'string'),
229 'first_name' => array ( 'name' => 'first_name', 'type' => 'string'),
230 'last_name' => array ( 'name' => 'last_name', 'type' => 'string'),
231 'email' => array ( 'name' => 'email', 'type' => 'string'),
232 'country' => array ( 'name' => 'country', 'type' => 'integer'),
233 'password' => array ( 'name' => 'password', 'type' => 'string'),
234 'created_at' => array ( 'name' => 'created_at', 'type' => 'datetime' ),
235 'updated_at' => array ( 'name' => 'updated_at', 'type' => 'datetime' ),
236 'expires_on' => array ( 'name' => 'expires_on', 'type' => 'date' ) );
238 $got = array();
239 $tmp_got = $AkTestUser->getColumns();
240 foreach ($tmp_got as $k=>$v){
241 $got[$k]['name'] = $v['name'];
242 $got[$k]['type'] = $v['type'];
243 if(!empty($v['primaryKey'])){
244 $got[$k]['primaryKey'] = $v['primaryKey'];
248 $this->assertEqual($got, $expected);
250 //---------------------
252 $AkTestField = new AkTestField();
253 $AkTestField->getColumns();
254 //$AkTestField->debug();
256 $expected = array(
257 'id'=>'serial',
258 'varchar_field'=>'string',
259 'longtext_field'=>'text',
260 'text_field'=>'text',
261 'logblob_field'=>'binary',
262 'date_field'=>'date',
263 'datetime_field'=>'datetime',
264 'tinyint_field'=>'integer',
265 'integer_field'=>'integer',
266 'smallint_field'=>'integer',
267 'bigint_field'=>'integer',
268 'double_field'=>'float',
269 'numeric_field'=>'decimal',
270 'bytea_field'=>'binary',
271 'timestamp_field'=>'datetime',
272 'boolean_field'=>'boolean',
273 'int2_field'=>'integer',
274 'int4_field'=>'integer',
275 'int8_field'=>'integer',
276 'foat_field'=>'float',
277 'varchar4000_field'=>'text',
278 'clob_field'=>'text',
279 'nvarchar2000_field'=>'text',
280 'blob_field'=>'binary',
281 'nvarchar_field'=>'string',
282 'decimal1_field'=>'boolean',
283 //'decimal3_field'=>'integer',
284 'decimal5_field'=>'integer',
285 'decimal10_field'=>'integer',
286 'decimal20_field'=>'integer',
287 'decimal_field'=>'decimal',
288 'created_at'=>'datetime',
289 'updated_at'=>'datetime',
290 'expires_on'=>'date'
294 $got = array();
295 foreach ($AkTestField->_columnsSettings as $k=>$v){
296 $got[$k] = $v['type'];
297 if($expected[$k] != $got[$k]){
298 //Ak::trace("$k => ".$expected[$k].' ::: '.$got[$k],__LINE__,__FILE__);
301 $this->assertEqual($got, $expected);
302 //$AkTestField->debug();
305 /// @todo implement test cases
307 // Test_of___construct(){}
308 // Test_of___destruct(){}
309 // Test_of__addMethod(){}
310 // Test_of__constructAssociatedModels(){}
311 // Test_of__getIncludedModelNames(){}
312 // Test_of__link(){}
313 // Test_of__linkAssociations(){}
314 // Test_of__update(){}
315 // Test_of__validateAssociatedModel(){}
319 /////// COMBINED ATTRIBUTES TESTS
321 function Test_of_addCombinedAttributeConfiguration()
323 $User = new AkTestUser();
324 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
325 $User->addCombinedAttributeConfiguration('reversed_name', "%s, %s", 'last_name', 'first_name');
326 // Checking that composeCombinedAttributes is called when new rules are added
327 $this->assertTrue(empty($User->name));
328 $this->assertTrue(empty($User->reversed_name));
330 $expected = array('name' => array('%s %s','first_name','last_name'),'reversed_name' => array('%s, %s','last_name','first_name'));
331 $this->assertEqual($User->_combinedAttributes, $expected);
334 function Test_of_composeCombinedAttribute()
336 $User = new AkTestUser();
337 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
338 $User->addCombinedAttributeConfiguration('reversed_name', "%s, %s", 'last_name', 'first_name');
339 $User->addCombinedAttributeConfiguration('reversed_name', array("%s, %s","%[^,], %s"), 'last_name', 'first_name');
340 $User->addCombinedAttributeConfiguration('email_link', array("callBackFunctionCompose","callBackFunctionDecompose"), 'email', 'name');
341 $User->addCombinedAttributeConfiguration('email_header_from', "<%s>%s", 'email', 'name');
343 $User->first_name = 'Bermi';
344 $User->last_name = 'Ferrer';
346 $User->composeCombinedAttribute('name');
347 $User->composeCombinedAttribute('reversed_name');
349 $this->assertEqual($User->name,'Bermi Ferrer');
350 $this->assertEqual($User->reversed_name,'Ferrer, Bermi');
352 $User->setAttribute('last_name', 'Ferrer');
353 $User->first_name = 'Bermi';
354 $this->assertEqual($User->getAttribute('name'), 'Bermi Ferrer');
356 $User->first_name = 'Hilario';
357 $User->set('last_name', 'Hervas');
358 $this->assertEqual($User->get('reversed_name'), 'Hervas, Hilario');
360 $User = new AkTestUser();
361 $User->addCombinedAttributeConfiguration(array('name', "%s %s", 'first_name', 'last_name')); // This is how combined attributes are added when they are set trhough a model variable
362 $User->first_name = 'Bermi';
363 $User->last_name = 'Ferrer';
365 $User->composeCombinedAttribute('name');
366 $this->assertEqual($User->name,'Bermi Ferrer');
370 function Test_of_decomposeCombinedAttribute()
373 $User = new AkTestUser();
374 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
375 $User->addCombinedAttributeConfiguration('reversed_name', "%[^,], %s", 'last_name', 'first_name');
376 $User->addCombinedAttributeConfiguration('email_link', array("callBackFunctionCompose","callBackFunctionDecompose"), 'email', 'name');
377 $User->addCombinedAttributeConfiguration('email_header_from', "<%s>%s", 'email', 'name');
379 $User->setAttribute('name', 'Bermi Ferrer');
380 $this->assertEqual($User->getAttribute('first_name'), 'Bermi');
381 $this->assertEqual($User->last_name, 'Ferrer');
383 $User->set('reversed_name', 'Hervas, Hilario');
384 $this->assertEqual($User->first_name, 'Hilario');
385 $this->assertEqual($User->last_name, 'Hervas');
387 $User->email_link = "<a href='mailto:nospam@example.com'>Bermi Ferrer</a>";
388 $User->decomposeCombinedAttribute('email_link');
389 $this->assertEqual($User->email, 'nospam@example.com');
390 $this->assertEqual($User->name, 'Bermi Ferrer');
392 $this->assertEqual($User->get('email_header_from'), '<nospam@example.com>Bermi Ferrer');
395 function Test_of_decomposeCombinedAttributes()
398 $User = new AkTestUser();
400 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
401 $User->addCombinedAttributeConfiguration('reversed_name', "%[^,], %s", 'last_name', 'first_name');
402 $User->addCombinedAttributeConfiguration('email_link', array("callBackFunctionCompose","callBackFunctionDecompose"), 'email', 'name');
403 $User->addCombinedAttributeConfiguration('email_header_from', "<%s>%s", 'email', 'name');
405 $User->name = 'Bermi Ferrer';
407 $User->decomposeCombinedAttributes();
408 $this->assertEqual($User->getAttribute('first_name'), 'Bermi');
409 $this->assertEqual($User->last_name, 'Ferrer');
411 $User->email_link = "<a href='mailto:nospam@example.com'>Hilario Hervas</a>";
412 $User->decomposeCombinedAttributes();
413 $this->assertEqual($User->first_name, 'Hilario');
414 $this->assertEqual($User->last_name, 'Hervas');
415 $this->assertEqual($User->name, 'Hilario Hervas');
416 $this->assertEqual($User->email, 'nospam@example.com');
419 $User->name = 'Wont Precede';
420 $User->email_link = "<a href='mailto:nospam-again@example.com'>Must Precede</a>";
421 $User->decomposeCombinedAttributes();
422 $this->assertEqual($User->getAttribute('name'), 'Must Precede');
423 $this->assertEqual($User->getAttribute('email'), 'nospam-again@example.com');
427 function Test_of_getAttribute()
429 $User = new AkTestUser();
431 $User->first_name = 'Bermi';
432 $User->password = 'pass';
433 $this->assertEqual($User->get('first_name'), 'Bermi');
434 $this->assertEqual($User->get('_columns'), null);
435 $this->assertEqual($User->get('password'),'*********');
437 $User->set('password', 'bermi');
438 $this->assertEqual($User->getAttributeBeforeTypeCast('password'),'c6dd746a20f85fecb18591f29508d42d');
442 function Test_of_getAvailableAttributes()
444 $User = new AkTestUser();
445 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
446 $tmp_got = $User->getAvailableAttributes();
447 $expected = array('name' => 'name', 'type' => 'string', 'path' => '%s %s','uses'=>array('first_name','last_name'));
448 $got = $tmp_got['name'];
449 $this->assertEqual($got, $expected);
451 $expected = array('name' => 'id', 'type' => 'serial', 'primaryKey' => 1);
452 $got = array('name' => $tmp_got['id']['name'], 'type' => $tmp_got['id']['type'], 'primaryKey' => $tmp_got['id']['primaryKey']);
453 $this->assertEqual($got, $expected);
455 $expected = array('name' => 'created_at', 'type' => 'datetime');
456 $got = array('name' => $tmp_got['created_at']['name'], 'type' => $tmp_got['created_at']['type']);
457 $this->assertEqual($got, $expected);
461 function Test_of_getAttributes()
463 $User = new AkTestUser();
465 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
467 $User->name = "Bermi Ferrer";
468 $User->email = "bermi@example.com";
469 $User->set('password','nada');
470 $User->decomposeCombinedAttributes();
471 $this->assertEqual($User->getAttributes(),array ( 'id' => NULL, 'user_name' => NULL, 'first_name' => 'Bermi', 'last_name' => 'Ferrer', 'email' => 'bermi@example.com', 'country' => NULL, 'password' => '*********', 'created_at' => NULL, 'updated_at' => NULL, 'expires_on' => NULL, 'name' => 'Bermi Ferrer' ));
475 function Test_of_getAttributeNames()
477 $User = new AkTestUser();
479 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
480 $this->assertEqual($User->getAttributeNames(),array ( 'country' => 'Country', 'created_at' => 'Created at', 'email' => 'Email', 'expires_on' => 'Expires on', 'first_name' => 'First name', 'id' => 'Id', 'last_name' => 'Last name', 'name' => 'Name', 'password' => 'Password', 'updated_at' => 'Updated at', 'user_name' => 'User name' ));
484 function Test_of_getAttributesBeforeTypeCast()
486 $User = new AkTestUser();
487 $User->set('password', 'bermi');
488 $this->assertEqual($User->getAttributesBeforeTypeCast(), array('password' => 'c6dd746a20f85fecb18591f29508d42d'));
489 $this->assertEqual($User->get('password'), '*********');
493 function Test_of_getOnlyAvailableAttributes()
495 $User = new AkTestUser();
496 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
497 $attributes = array('name'=>'Bermi Ferrer', 'email' => 'bermi@example.com', 'invalid_field'=>'value');
498 $attributes = $User->getOnlyAvailableAttributes($attributes);
499 $this->assertEqual($attributes,array('name'=>'Bermi Ferrer', 'email' => 'bermi@example.com'));
502 function Test_of_getColumnsForAttributes()
504 $User = new AkTestUser();
505 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
506 $attributes = array('name'=>'Bermi Ferrer', 'email' => 'bermi@example.com', 'invalid_field'=>'value');
507 $attributes = $User->getColumnsForAttributes($attributes);
508 $this->assertEqual($attributes,array('email' => 'bermi@example.com'));
512 function Test_of_hasAttribute()
514 $User = new AkTestUser();
515 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
516 $this->assertTrue($User->hasAttribute('name'));
517 $this->assertTrue($User->hasAttribute('first_name'));
518 $this->assertTrue($User->hasAttribute('id'));
519 $this->assertTrue($User->hasAttribute('expires_on'));
520 $this->assertFalse($User->hasAttribute('this_is_not_a_column_name'));
521 $this->assertFalse($User->hasAttribute('_columns'));
524 function Test_of_isAttributePresent()
526 $User = new AkTestUser();
527 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
529 $this->assertFalse($User->isAttributePresent('name'));
531 $User->set('name','Bermi Ferrer');
532 $this->assertTrue($User->isAttributePresent('name'));
533 $this->assertTrue($User->isAttributePresent('first_name'));
535 $User->set('last_name',null);
536 $this->assertFalse($User->isAttributePresent('last_name'));
538 $User->set('first_name','');
539 $this->assertFalse($User->isAttributePresent('first_name'));
542 function Test_of_getAvailableAttributesQuoted()
544 $User = new AkTestUser();
546 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
548 $fields = array('name'=>"Tim O'Reilly",'user_name'=>"a'''''a",'id'=>555,'expires_on'=>'2089-06-16');
549 $User->set($fields);
550 $expected = array('id' => "id=555",
551 'user_name' => "user_name='a\'\'\'\'\'a'",
552 'first_name' => "first_name='Tim'",
553 'last_name' => "last_name='O\'Reilly'",
554 'email' => "email=null",
555 'country' => "country=null",
556 'password' => "password='*********'",
557 'created_at' => "created_at=null",
558 'updated_at' => "updated_at=null",
559 'expires_on' => "expires_on='2089-06-16'"
562 $got = $User->getAvailableAttributesQuoted();
563 $this->assertTrue($expected['last_name'] == $got['last_name'] || str_replace("\\","'",$expected['last_name']) == $got['last_name']);
564 $this->assertTrue($expected['user_name'] == $got['user_name'] || str_replace("\\","'",$expected['user_name']) == $got['user_name']);
565 unset($expected['user_name'], $got['user_name'], $expected['last_name'], $got['last_name']);
566 $this->assertEqual($expected, $got);
569 function Test_of_getSerializedAttributes_and_setSerializeAttribute()
571 $User = new AkTestUser();
572 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
573 $User->setSerializeAttribute('user_name');
574 $User->setSerializeAttribute('name');
575 $User->setSerializeAttribute('not_valid');
576 $User->setSerializeAttribute('email','MimeEmail');
577 $expected = array('user_name' => null, 'email' => 'MimeEmail');
578 $this->assertEqual($expected, $User->getSerializedAttributes());
582 function Test_of_setAccessibleAttributes()
584 $User = new AkTestUser();
585 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
586 $User->setAccessibleAttributes('first_name','last_name','name','country','email');
587 $expected = array('first_name', 'last_name', 'name', 'country', 'email');
588 $this->assertEqual($expected, $User->_accessibleAttributes);
592 function Test_of_setProtectedAttributes()
594 $User = new AkTestUser();
595 $User->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
596 $User->setProtectedAttributes('first_name','last_name','name','country','email');
597 $expected = array('first_name', 'last_name', 'name', 'country', 'email');
598 $this->assertEqual($expected, $User->_protectedAttributes);
601 function Test_of_setAttribute()
603 $User = new AkTestUser();
604 $User->setAttribute('first_name', 'Bermi');
605 $User->setAttribute('password', 'pass');
606 $this->assertEqual($User->first_name, 'Bermi');
607 $this->assertFalse($User->setAttribute('_columns',array()));
608 $this->assertFalse(empty($User->_columns));
609 $this->assertEqual($User->get('password'),'*********');
610 $User->set('password', 'bermi');
611 $this->assertEqual($User->getAttributeBeforeTypeCast('password'),'c6dd746a20f85fecb18591f29508d42d');
614 function Test_of_setAttributes()
616 $User = new AkTestUser();
617 $attributes = array(
618 'first_name'=> 'Bermi', 'password'=> 'bermi', '_columns'=>array(), '_test_private_var'=>true
620 $User->setAttributes($attributes);
621 $this->assertFalse(empty($User->_columns));
622 $this->assertEqual($User->first_name, 'Bermi');
623 $this->assertEqual($User->get('password'),'*********');
624 $this->assertEqual($User->getAttributeBeforeTypeCast('password'),'c6dd746a20f85fecb18591f29508d42d');
626 $User->setAttributes($attributes,true);
627 $this->assertFalse(!empty($User->_test_private_var));
630 function Test_of_toggleAttribute()
632 $AkTestField = new AkTestField();
633 $AkTestField->set('boolean_field', true);
634 $this->assertTrue($AkTestField->boolean_field);
635 $this->assertTrue($AkTestField->get('boolean_field'));
636 $AkTestField->toggleAttribute('boolean_field');
637 $this->assertTrue(!$AkTestField->boolean_field);
638 $this->assertTrue(!$AkTestField->get('boolean_field'));
639 $AkTestField->toggleAttribute('boolean_field');
640 $this->assertTrue($AkTestField->boolean_field);
641 $this->assertTrue($AkTestField->get('boolean_field'));
644 function Test_of_get_and_set_DisplayField()
646 $AkTestField = new AkTestField();
647 $this->assertEqual($AkTestField->getDisplayField(), 'id');
648 $AkTestField->setDisplayField('text_field');
649 $this->assertEqual($AkTestField->getDisplayField(), 'text_field');
651 $AkTestUser = new AkTestUser();
652 $this->assertEqual($AkTestUser->getDisplayField(), 'id');
654 $AkTestUser->addCombinedAttributeConfiguration('name', "%s %s", 'first_name', 'last_name');
655 $this->assertEqual($AkTestUser->getDisplayField(), 'name');
657 $AkTestField->setDisplayField('invalid_field');
658 $this->assertEqual($AkTestUser->getDisplayField(), 'name');
661 function Test_of_get_and_get_Id()
663 $AkTestField = new AkTestField();
664 $this->assertEqual($AkTestField->getId(), null);
666 $AkTestField->setId(123);
667 $this->assertEqual($AkTestField->getId(), 123);
669 $AkTestField->incrementAttribute($AkTestField->getPrimaryKey());
670 $this->assertEqual($AkTestField->getId(), 124);
672 /**/
677 ak_test('test_AkActiveRecord',true);