/** * Install hstore * /usr/share/postgresql/contrib # cat hstore.sql | psql -U pgsql -d onphp **/ public function testHstore() { foreach (DBTestPool::me()->getPool() as $connector => $db) { DBPool::me()->setDefault($db); $properties = array('age' => '23', 'weight' => 80, 'comment' => null); $user = TestUser::create()->setCity($moscow = TestCity::create()->setName('Moscow'))->setCredentials(Credentials::create()->setNickname('fake')->setPassword(sha1('passwd')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())->modify('-1 day'))->setProperties(Hstore::make($properties)); $moscow = TestCity::dao()->add($moscow); $user = TestUser::dao()->add($user); Cache::me()->clean(); TestUser::dao()->dropIdentityMap(); $user = TestUser::dao()->getById('1'); $this->assertInstanceOf('Hstore', $user->getProperties()); $this->assertEquals($properties, $user->getProperties()->getList()); $form = TestUser::proto()->makeForm(); $form->get('properties')->setFormMapping(array(Primitive::string('age'), Primitive::integer('weight'), Primitive::string('comment'))); $form->import(array('id' => $user->getId())); $this->assertNotNull($form->getValue('id')); $object = $user; FormUtils::object2form($object, $form); $this->assertInstanceOf('Hstore', $form->getValue('properties')); $this->assertEquals(array_filter($properties), $form->getValue('properties')->getList()); $subform = $form->get('properties')->getInnerForm(); $this->assertEquals($subform->getValue('age'), '23'); $this->assertEquals($subform->getValue('weight'), 80); $this->assertNull($subform->getValue('comment')); $user = new TestUser(); FormUtils::form2object($form, $user, false); $this->assertEquals($user->getProperties()->getList(), array_filter($properties)); } }
public function testRemoveUserRole() { $user = TestUser::create(['name' => 'user']); $role = Role::create(['name' => 'role']); $user->addRole($role); $user->removeRole($role); $this->assertFalse($user->roles->contains($role->getKey())); $this->assertFalse($role->users->contains($user->getKey())); }
/** * testDeleteAdminUser method * * @return void */ public function testDeleteAdminUser() { $this->User->create(array('username' => 'admin_user', 'name' => 'Admin User', 'role_id' => 1, 'email' => '*****@*****.**', 'password' => 'password', 'website' => 'http://croogo.org', 'activation_key' => md5(uniqid()), 'status' => true)); $this->User->save(); $newAdmin = $this->User->read(); $this->User->create(array('username' => 'another_adm', 'name' => 'Another Admin', 'role_id' => 1, 'email' => '*****@*****.**', 'password' => 'password', 'website' => 'http://croogo.org', 'activation_key' => md5(uniqid()), 'status' => true)); $this->User->save(); $anotherAdmin = $this->User->read(); $this->User->deleteAll(array('NOT' => array('User.id' => array($newAdmin['User']['id'], $anotherAdmin['User']['id'])))); $this->assertTrue($this->User->delete($newAdmin['User']['id'])); }
public function testUserPermissionComesBeforeRolePermission() { $user = TestUser::create(['name' => 'user']); $role = Role::create(['name' => 'role']); $perm = Permission::create(['name' => 'permission']); $user->addRole($role); $role->allowPermission($perm); $user->denyPermission($perm); $perm = $user->getPermission($perm); $this->assertNotNull($perm); $this->assertFalse($perm->allow); }
public function testCount() { foreach (DBTestPool::me()->getPool() as $db) { DBPool::me()->setDefault($db); $this->getDBCreator()->fillDB(); $count = TestUser::dao()->getTotalCount(); $this->assertGreaterThan(1, $count); $city = TestCity::create()->setId(1); $newUser = TestUser::create()->setCity($city)->setCredentials(Credentials::create()->setNickname('newuser')->setPassword(sha1('newuser')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())); TestUser::dao()->add($newUser); $newCount = TestUser::dao()->getTotalCount(); $this->assertEquals($count + 1, $newCount); } }
public function testIpAddressProperty() { foreach (DBTestPool::me()->getPool() as $db) { DBPool::me()->setDefault($db); $city = TestCity::create()->setName('Khimki'); TestCity::dao()->add($city); $userWithIp = TestUser::create()->setCredentials(Credentials::create()->setNickName('postgreser')->setPassword(sha1('postgreser')))->setLastLogin(Timestamp::makeNow())->setRegistered(Timestamp::makeNow())->setCity($city)->setIp(IpAddress::create('127.0.0.1')); TestUser::dao()->add($userWithIp); $this->assertTrue($userWithIp->getId() >= 1); $this->assertTrue($userWithIp->getIp() instanceof IpAddress); $plainIp = DBPool::me()->getByDao(TestUser::dao())->queryColumn(OSQL::select()->get('ip')->from(TestUser::dao()->getTable())->where(Expression::eq('id', $userWithIp->getId()))); $this->assertEquals($plainIp[0], $userWithIp->getIp()->toString()); $count = Criteria::create(TestUser::dao())->add(Expression::eq('ip', IpAddress::create('127.0.0.1')))->addProjection(Projection::count('*', 'count'))->getCustom('count'); $this->assertEquals($count, 1); } }
public function testBind() { $user = TestUser::create()->setId(1); $bindingsList = array(array(1 => 1.123), array(1 => -1), array(1 => 'test'), array(1 => $user), array(1 => SQLFunction::create('rand'))); foreach ($bindingsList as $bindings) { $value = $bindings[1]; if ($value instanceof Identifiable) { $value = $value->getId(); } $this->assertCriteria('$1 from TestUser', Criteria::create(TestUser::dao())->setProjection(Projection::property($value)), $bindings)->assertCriteria('count($1) from TestUser', Criteria::create(TestUser::dao())->setProjection(Projection::count($value)), $bindings)->assertCriteria('from TestUser where id = $1', Criteria::create(TestUser::dao())->add(Expression::eq('id', $value)), $bindings); // in 'in' expression if (is_scalar($value)) { $this->assertCriteria('from TestUser where id in (1, $1)', Criteria::create(TestUser::dao())->add(Expression::in('id', array(1, $value))), $bindings); } $this->assertCriteria('from TestUser order by $1', Criteria::create(TestUser::dao())->addOrder(OrderBy::create($value)), $bindings)->assertCriteria('from TestUser having id = $1', Criteria::create(TestUser::dao())->setProjection(Projection::having(Expression::eq('id', $value))), $bindings)->assertCriteria('from TestUser group by id = $1', Criteria::create(TestUser::dao())->setProjection(Projection::group(Expression::eq('id', $value))), $bindings); if (is_integer($value) && $value >= 0) { $this->assertCriteria('from TestUser limit $1', Criteria::create(TestUser::dao())->setLimit($value), $bindings)->assertCriteria('from TestUser offset $1', Criteria::create(TestUser::dao())->setOffset($value), $bindings); } } }
protected function loginClient() { $user = TestUser::create(); auth()->login($user); return $user; }
/** * @return TestUser */ private function spawnUser($options = array()) { $options += array('id' => '77', 'credentials' => Credentials::create(), 'lastLogin' => Timestamp::create('2011-12-31'), 'registered' => Timestamp::create('2011-12-30'), 'strangeTime' => Time::create('01:23:45'), 'city' => null, 'firstOptional' => null, 'secondOptional' => null, 'url' => HttpUrl::create()->parse('https://www.github.com'), 'properties' => Hstore::make(array('a' => 'apple', 'b' => 'bananas')), 'ip' => IpAddress::create('127.0.0.1')); return $this->spawnObject(TestUser::create(), $options); }
protected function createFakes() { return [TestUser::create(['name' => 'user']), Role::create(['name' => 'role']), Permission::create(['name' => 'permission']), TestResource::create(['name' => 'resource'])]; }
/** * @dataProvider exampleUser */ public function testCreateSendsParams($user) { TestUser::create($user); $request = Castle_RequestTransport::getLastRequest(); $this->assertEquals($user, $request['params']); }
/** * @param TestCase $test * @return DBTestCreator */ public function fillDB(TestCase $test = null) { $moscow = TestCity::create()->setName('Moscow'); $piter = TestCity::create()->setName('Saint-Peterburg'); $mysqler = TestUser::create()->setCity($moscow)->setCredentials(Credentials::create()->setNickname('mysqler')->setPassword(sha1('mysqler')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())->modify('-1 day')); $postgreser = clone $mysqler; $postgreser->setCredentials(Credentials::create()->setNickName('postgreser')->setPassword(sha1('postgreser')))->setCity($piter)->setUrl(HttpUrl::create()->parse('http://postgresql.org/')); $piter = TestCity::dao()->add($piter); $moscow = TestCity::dao()->add($moscow); if ($test) { $test->assertEquals($piter->getId(), 1); $test->assertEquals($moscow->getId(), 2); } $postgreser = TestUser::dao()->add($postgreser); for ($i = 0; $i < 10; $i++) { $encapsulant = TestEncapsulant::dao()->add(TestEncapsulant::create()->setName($i)); $encapsulant->getCities()->fetch()->setList(array($piter, $moscow))->save(); } $mysqler = TestUser::dao()->add($mysqler); if ($test) { $test->assertEquals($postgreser->getId(), 1); $test->assertEquals($mysqler->getId(), 2); } if ($test) { // put them in cache now TestUser::dao()->dropIdentityMap(); TestUser::dao()->getById(1); TestUser::dao()->getById(2); if ($test instanceof DBDataTest) { $test->getListByIdsTest(); } Cache::me()->clean(); $test->assertTrue($postgreser == TestUser::dao()->getById(1)); $test->assertTrue($mysqler == TestUser::dao()->getById(2)); } $firstClone = clone $postgreser; $secondClone = clone $mysqler; $firstCount = TestUser::dao()->dropById($postgreser->getId()); $secondCount = TestUser::dao()->dropByIds(array($mysqler->getId())); if ($test) { $test->assertEquals($firstCount, 1); $test->assertEquals($secondCount, 1); try { TestUser::dao()->getById(1); $test->fail(); } catch (ObjectNotFoundException $e) { /* pass */ } $result = Criteria::create(TestUser::dao())->add(Expression::eq(1, 2))->getResult(); $test->assertEquals($result->getCount(), 0); $test->assertEquals($result->getList(), array()); } TestUser::dao()->import($firstClone); TestUser::dao()->import($secondClone); if ($test && $test instanceof DBDataTest) { // cache multi-get $test->getListByIdsTest(); $test->getListByIdsTest(); } return $this; }
/** * @expectedException RuntimeException */ public function testUserPermissionActionAttribute() { $user = TestUser::create(['name' => 'user']); $perm = Permission::create(['name' => 'permission']); $user->allowPermission($perm); $perm = $user->mergedPermissions()->first(); $var = $perm->action; }