public function test_basic_operation() { $date = new Date(); FuzzyTest::assert_equal($date->year, date("Y"), "Date created with wrong year"); FuzzyTest::assert_equal($date->month, date("m"), "Date created with wrong month"); FuzzyTest::assert_equal($date->day, date("d"), "Date created with wrong day"); FuzzyTest::assert_equal($date->hour, date("H"), "Date created with wrong hour"); FuzzyTest::assert_equal($date->minute, date("i"), "Date created with wrong minute"); FuzzyTest::assert_equal($date->second, date("s"), "Date created with wrong second"); FuzzyTest::assert_equal($date->timezone_offset, "+0000", "Date created with timezone offset"); $date = new Date(mktime()); FuzzyTest::assert_equal($date->year, date("Y"), "Date created with wrong year"); FuzzyTest::assert_equal($date->month, date("m"), "Date created with wrong month"); FuzzyTest::assert_equal($date->day, date("d"), "Date created with wrong day"); FuzzyTest::assert_equal($date->hour, date("H"), "Date created with wrong hour"); FuzzyTest::assert_equal($date->minute, date("i"), "Date created with wrong minute"); FuzzyTest::assert_equal($date->second, date("s"), "Date created with wrong second"); FuzzyTest::assert_equal($date->timezone_offset, "+0000", "Date created with timezone offset"); $date = new Date(date("d F Y H:i:s")); FuzzyTest::assert_equal($date->year, date("Y"), "Date created with wrong year"); FuzzyTest::assert_equal($date->month, date("m"), "Date created with wrong month"); FuzzyTest::assert_equal($date->day, date("d"), "Date created with wrong day"); FuzzyTest::assert_equal($date->hour, date("H"), "Date created with wrong hour"); FuzzyTest::assert_equal($date->minute, date("i"), "Date created with wrong minute"); FuzzyTest::assert_equal($date->second, date("s"), "Date created with wrong second"); FuzzyTest::assert_equal($date->timezone_offset, "+0000", "Date created with timezone offset"); $date = new Date(date("c")); FuzzyTest::assert_equal($date->year, date("Y"), "Date created with wrong year"); FuzzyTest::assert_equal($date->month, date("m"), "Date created with wrong month"); FuzzyTest::assert_equal($date->day, date("d"), "Date created with wrong day"); FuzzyTest::assert_equal($date->hour, date("H"), "Date created with wrong hour"); FuzzyTest::assert_equal($date->minute, date("i"), "Date created with wrong minute"); FuzzyTest::assert_equal($date->second, date("s"), "Date created with wrong second"); FuzzyTest::assert_equal($date->timezone_offset, "+0000", "Date created with timezone offset"); }
public function test_belongs_to() { MemoryStore::flush(); Page::delete_all(); User::delete_all(); $user = new User(); $user->email = "*****@*****.**"; $user->first_name = "Ben"; $user->last_name = "Copsey"; $user->password = "******"; $user->accepted_terms_and_conditions = true; $user->registration_date = new Date(); $user->first_name = "Ben"; $user->save(); $page1 = new Page(); $page1->title = "This is page 1"; $page1->last_modified = new Date(); $page1->body = "This is the content"; $page1->url = "page-1"; $page1->author = $user; $page1->save(); FuzzyTest::assert_equal($page1->author_id, $user->id, "Author not set correctly"); $user->delete(); $page = Page::find_by_url('page-1'); FuzzyTest::assert_true(isset($page), "Page deleted when it should have been preserved"); FuzzyTest::assert_equal($page->author_id, 0, "Page deleted when it should have been preserved"); $user->save(); $page->author = $user; $page->save(); $matches = $user->pages; FuzzyTest::assert_equal(count($matches), 1, "Page count should be 1"); }
public function test_sorter() { $page = Page::find_by_url("page-2"); FuzzyTest::assert_equal($page->position, 2, "Page was created in the wrong position"); $page->move_down('position'); FuzzyTest::assert_equal($page->position, 3, "Page was moved to the wrong position"); $page->move_up('position'); FuzzyTest::assert_equal($page->position, 2, "Page was moved to the wrong position"); $page->move_up('position'); FuzzyTest::assert_equal($page->position, 1, "Page was moved to the wrong position"); $page = Page::find_by_url("page-3"); FuzzyTest::assert_equal($page->position, 2, "Page was moved to the wrong position"); $page = Page::find_by_url("page-1"); FuzzyTest::assert_equal($page->position, 3, "Page was moved to the wrong position"); }
public function test_file_field_read_and_write() { $file = new File("/tmp/test.txt"); $user = User::find_by_email("*****@*****.**"); $document = new Document(); $document->user = $user; $document->last_modified = new Date(); $document->file = $file; $document->save(); FuzzyTest::assert_true($file->exists(), "File not written"); $document = Document::find_by_id($document->id); $result = is_a($document->file, "File"); FuzzyTest::assert_true($result, "File not read correctly"); $contents = $document->file->read(); FuzzyTest::assert_equal($contents, "This is the content of the file", "File not read correctly"); }
public function test_has_many() { UserLogin::delete_all(); User::delete_all(); $user = new User(); $user->email = "*****@*****.**"; $user->password = "******"; $user->accepted_terms_and_conditions = true; $user->registration_date = new Date(); $user->first_name = "Ben"; $user->last_name = "Copsey"; $user->save(); $user = new User(); $user->email = "*****@*****.**"; $user->password = "******"; $user->accepted_terms_and_conditions = true; $user->registration_date = new Date(); $user->first_name = "Ben"; $user->last_name = "Copsey"; $user->save(); $user = User::find_by_email('*****@*****.**'); $user_login = new UserLogin(); $user_login->user_id = $user->id; $user_login->date = new Date(); $user_login->ip_address = "127.0.0.1"; $user_login->save(); $matches = $user->logins(); FuzzyTest::assert_equal(count($matches), 1, "Should find one login here"); $user_login = new UserLogin(); $user_login->user_id = $user->id; $user_login->date = new Date(); $user_login->ip_address = "127.0.0.1"; $user_login->save(); $matches = UserLogin::find_all_by_user($user); FuzzyTest::assert_equal(count($matches), 2, "Should find two logins here"); $matches = UserLogin::find_all_where("user_id = " . DB::escape($user->id)); FuzzyTest::assert_equal(count($matches), 2, "Should find two logins here"); $matches = UserLogin::find_all_where("user_id = " . DB::escape($user->id), 1); FuzzyTest::assert_equal(count($matches), 1, "Should find one login here"); $login = $matches[0]; $matches = UserLogin::find_all_where("user_id = " . DB::escape($user->id), 2); $login2 = $matches[0]; FuzzyTest::assert_equal($login->id, $login2->id, "Two logins should be equal"); $matches = UserLogin::find_all_where("user_id = " . DB::escape($user->id), 0, 2); $login2 = $matches[0]; FuzzyTest::assert_equal($login->id, $login2->id, "Two logins should be equal"); $matches = UserLogin::find_all_where("user_id = " . DB::escape($user->id), 1, 2); $login2 = $matches[0]; FuzzyTest::assert_not_equal($login->id, $login2->id, "Two logins should be equal"); $matches = $user->logins(); FuzzyTest::assert_equal(count($matches), 2, "Should find two logins here"); $login = UserLogin::find_by_user_id($user->id); FuzzyTest::assert_equal($login->user_id, $user->id, "Found wrong login"); $login = UserLogin::find_by_user($user); FuzzyTest::assert_equal($login->user_id, $user->id, "Found wrong login"); $u = User::find_by_login($user_login); FuzzyTest::assert_equal($u->id, $user->id, "Found wrong user"); $user_login = new UserLogin(); $user_login->user = $user; $user_login->date = new Date(); $user_login->ip_address = "127.0.0.1"; $user_login->save(); $matches = $user->logins(); FuzzyTest::assert_equal(count($matches), 3, "Should find three logins here"); $user->delete(); $matches = UserLogin::find_by_user_id($user->id); FuzzyTest::assert_equal(count($matches), 0, "Should find zero logins here"); $matches = UserLogin::find_by_user($user); FuzzyTest::assert_equal(count($matches), 0, "Should find zero logins here"); UserLogin::delete_all(); $user_login1 = new UserLogin(); $user_login1->date = new Date(); $user_login1->ip_address = "127.0.0.1"; $user_login2 = new UserLogin(); $user_login2->date = new Date(); $user_login2->ip_address = "127.0.0.1"; //$user->save(); $user->logins = array($user_login1, $user_login2); $user->save(); $matches = $user->logins; FuzzyTest::assert_equal(count($matches), 2, "Should find two logins here"); $user2 = new User(); $user2 = User::find_by_email('*****@*****.**'); $user_login1->user = $user2; $user_login1->save(); $matches = $user->logins; FuzzyTest::assert_equal(count($matches), 1, "Should find one login here"); $matches = $user2->logins(); FuzzyTest::assert_equal(count($matches), 1, "Should find one login here"); }
public function test_basic_find() { $count = User::count(); FuzzyTest::assert_equal($count, 3, "Should find three users here"); $matches = User::find_all(); FuzzyTest::assert_equal(count($matches), 3, "Should find three users here"); $count = User::count(array('email' => '*****@*****.**')); FuzzyTest::assert_equal($count, 1, "Should find one user here"); $matches = User::find(array('email' => '*****@*****.**')); FuzzyTest::assert_equal(count($matches), 1, "Should find one user here"); $u = $matches[0]; FuzzyTest::assert_equal($u->email, "*****@*****.**", "Found wrong user"); $count = User::count(array('first_name' => 'Ben')); FuzzyTest::assert_equal($count, 2, "Should find two users here"); $matches = User::find(array('first_name' => 'Ben')); FuzzyTest::assert_equal(count($matches), 2, "Should find two users here"); $matches = User::find_all_by_first_name('Ben'); FuzzyTest::assert_equal(count($matches), 2, "Should find two users here"); $count = User::count_by_first_name('Ben'); FuzzyTest::assert_equal($count, 2, "Should find two users here"); $matches = User::find_all_by_email('*****@*****.**'); FuzzyTest::assert_equal(count($matches), 1, "Should find one user here"); $count = User::count_by_email('*****@*****.**'); FuzzyTest::assert_equal($count, 1, "Should find one user here"); $u = $matches[0]; FuzzyTest::assert_equal($u->email, "*****@*****.**", "Found wrong user"); $u = User::find_by_email('*****@*****.**'); FuzzyTest::assert_equal($u->email, "*****@*****.**", "Found wrong user"); $matches = User::find_all_by_email_and_first_name('*****@*****.**', 'Ben'); FuzzyTest::assert_equal(count($matches), 1, "Should find one user here"); $count = User::count_by_email_and_first_name('*****@*****.**', 'Ben'); FuzzyTest::assert_equal($count, 1, "Should find one user here"); $u = $matches[0]; FuzzyTest::assert_equal($u->email, "*****@*****.**", "Found wrong user"); $matches = User::find(array('first_name' => 'Ben', 'limit' => 1)); FuzzyTest::assert_equal(count($matches), 1, "Should find one user here"); $matches = User::find(array('first_name' => 'Ben', 'order_by' => 'registration_date')); FuzzyTest::assert_equal(count($matches), 2, "Should find two users here"); $u = $matches[0]; FuzzyTest::assert_equal($u->email, "*****@*****.**", "Sorted results in the wrong order"); $matches = User::find(array('first_name' => 'Ben', 'order_by' => 'email', 'sort' => "descending")); FuzzyTest::assert_equal(count($matches), 2, "Should find two users here"); $u = $matches[0]; FuzzyTest::assert_equal($u->email, "*****@*****.**", "Sorted results in the wrong order"); $matches = User::find(array('first_name' => 'Ben', 'order_by' => 'email', 'sort' => "ascending")); FuzzyTest::assert_equal(count($matches), 2, "Should find two users here"); $u = $matches[1]; FuzzyTest::assert_equal($u->email, "*****@*****.**", "Found wrong user"); $matches = User::find(array('email_not' => '*****@*****.**')); FuzzyTest::assert_equal(count($matches), 2, "Should find two users here"); }