/** * Test * * @test * @return void */ public function createNew() { $user = new User(); $this->assertEquals('Jane', $user->getFirstname()); $this->assertEquals('Doe Doe', $user->getLastname()); $this->assertEquals('', $user->getCreated()); $this->assertEquals('', $user->getOwner()); }
public function testCreate() { $user = new User(); $user->setId(1); $user->setEmail('*****@*****.**'); $user->setPassword('xxxx'); $user->setFirstName('John'); $user->setLastName('Doe'); $user->addRole(new UserRole()); $user->addVehicle(new Vehicle()); $this->assertSame(null, $user->getUpdated()); $user->setUpdated(); $user->preUpdate(); $this->assertSame(1, $user->getId()); $this->assertTrue($user->getCreated() instanceof \DateTime); $this->assertTrue($user->getUpdated() instanceof \DateTime); $this->assertSame('*****@*****.**', $user->getEmail()); $this->assertSame('John', $user->getFirstName()); $this->assertSame('Doe', $user->getLastName()); $this->assertTrue($user->getRoles()[0] instanceof UserRole); $this->assertTrue($user->getVehicles()[0] instanceof Vehicle); }
/** * test #14. * Overriding automatic attributes using a future date. Because * a future date is used, the record can no longer be changed after * it was saved. * @depends testSaveEmptyObject * @depends testSetAttributes * @depends testUpdate * @depends testOverrideAutomaticAttributesNewRecord */ public function testOverrideAutomaticAttributesFutureDate() { global $testUserId1; // Create the object, which automatically gets the current date $object = new User($testUserId1); $object->setName('Test User'); $object->setExternalType('externaltype'); $object->setExternalId('externalid'); $object->setAccess('access'); $object->setEmail('*****@*****.**'); $object->setNotification('Y'); $object->setTempCode('tempcode'); $object->setDeleted('Y'); $this->assertTrue($object->save()); $this->assertEquals(1, $this->countTestRows()); $originalCreated = $object->getCreated(); $originalUpdated = $object->getUpdated(); $originalHash = $object->getHash(); // Change the object with different values, using a guaranteed // future date for the Created and Updated fields. Note that // the mySQL timestamp values allow for dates up to January 19, // 2038. Select as the future date for this test January 18, 2038 // values after first save are unchanged $object->setCreated('2038-01-18 10:10:10.000000'); $object->setUpdated('2038-01-18 10:10:11.000000'); $object->setName('Test User 2'); $object->setExternalType('externaltype 2'); $object->setExternalId('externalid 2'); $object->setAccess('access 2'); $object->setEmail('*****@*****.**'); $object->setNotification('N'); $object->setTempCode('tempcode 2'); $object->setDeleted('N'); $object->setHash('future date hash'); // Check the values before saving $this->assertEquals($testUserId1, $object->getUserId()); $this->assertEquals('2038-01-18 10:10:10.000000', $object->getCreated()); $this->assertEquals('2038-01-18 10:10:11.000000', $object->getUpdated()); $this->assertEquals('Test User 2', $object->getName()); $this->assertEquals('externaltype 2', $object->getExternalType()); $this->assertEquals('externalid 2', $object->getExternalId()); $this->assertEquals('access 2', $object->getAccess()); $this->assertEquals('*****@*****.**', $object->getEmail()); $this->assertEquals('N', $object->getNotification()); $this->assertEquals('tempcode 2', $object->getTempCode()); $this->assertEquals('N', $object->getDeleted()); $this->assertEquals('future date hash', $object->getHash()); // update the record, this adds a row in the database $this->assertTrue($object->save()); $this->assertEquals(2, $this->countTestRows()); // after the update, the information has been saved $this->assertEquals($testUserId1, $object->getUserId()); $this->assertEquals('2038-01-18 10:10:10.000000', $object->getCreated()); $this->assertEquals('2038-01-18 10:10:11.000000', $object->getUpdated()); $this->assertEquals('Test User 2', $object->getName()); $this->assertEquals('externaltype 2', $object->getExternalType()); $this->assertEquals('externalid 2', $object->getExternalId()); $this->assertEquals('access 2', $object->getAccess()); $this->assertEquals('*****@*****.**', $object->getEmail()); $this->assertEquals('N', $object->getNotification()); $this->assertEquals('tempcode 2', $object->getTempCode()); $this->assertEquals('N', $object->getDeleted()); $this->assertEquals('future date hash', $object->getHash()); // Try to update the record. This will add a row in the database $object->setName('Test User 3'); $object->setExternalType('externaltype 3'); $object->setExternalId('externalid 3'); $object->setAccess('access 3'); $object->setEmail('*****@*****.**'); $object->setNotification('Y'); $object->setTempCode('tempcode 3'); $object->setDeleted('Y'); $this->assertTrue($object->save()); $this->assertEquals(3, $this->countTestRows()); // but the new information is not saved. The previously saved // information cannot be overwritten without manually setting the // updated field. $this->assertEquals($testUserId1, $object->getUserId()); $this->assertEquals('2038-01-18 10:10:10.000000', $object->getCreated()); $this->assertEquals('2038-01-18 10:10:11.000000', $object->getUpdated()); $this->assertEquals('Test User 2', $object->getName()); $this->assertEquals('externaltype 2', $object->getExternalType()); $this->assertEquals('externalid 2', $object->getExternalId()); $this->assertEquals('access 2', $object->getAccess()); $this->assertEquals('*****@*****.**', $object->getEmail()); $this->assertEquals('N', $object->getNotification()); $this->assertEquals('tempcode 2', $object->getTempCode()); $this->assertEquals('N', $object->getDeleted()); // Note: this will FAIL in the current implementation! //$this->assertEquals('future date hash', $object->getHash()); }
/** * Test #13. SYNCH request write new object. */ public function testSynchPut() { global $testUserId1; global $synchAuthToken; $this->assertEquals(0, $this->countTestRows()); $data = array('userId' => $testUserId1, 'created' => '2015-10-01', 'updated' => '2015-10-02', 'name' => 'Test User', 'externalType' => 'externaltype', 'externalId' => 'externalid', 'access' => 'Y', 'email' => '*****@*****.**', 'notification' => 'Y', 'tempCode' => 'tempcode', 'deleted' => 'Y', 'hash' => 'forced hash'); $result = putApi('synchUser.php', $data, $synchAuthToken); $this->assertEquals(RESPONSE_SUCCESS, $result['resultCode']); $this->assertEquals(1, $this->countTestRows()); $object = new User($testUserId1); $this->assertEquals('2015-10-01 00:00:00.000000', $object->getCreated()); $this->assertEquals('2015-10-02 00:00:00.000000', $object->getUpdated()); $this->assertEquals("Test User", $object->getName()); $this->assertEquals("externaltype", $object->getExternalType()); $this->assertEquals("externalid", $object->getExternalId()); $this->assertEquals("Y", $object->getAccess()); $this->assertEquals("*****@*****.**", $object->getEmail()); $this->assertEquals("Y", $object->getNotification()); $this->assertEquals("tempcode", $object->getTempCode()); $this->assertEquals("Y", $object->getDeleted()); $this->assertEquals('forced hash', $object->getHash()); }
include_once dirname(__FILE__) . '/../database/User.php'; if (isPutMethod()) { $data = getPostData(); $userId = ''; if (isset($data['userId'])) { $userId = $data['userId']; } $password = ''; if (isset($data['password'])) { $password = $data['password']; } if ($userId === '' || $password === '') { $response = errorResponse(RESPONSE_BAD_REQUEST); } else { $user = new User($userId); if ($user->getCreated() === null) { $response = errorResponse(RESPONSE_NOT_FOUND); } else { if ($user->checkPassword($password)) { // password matches, need to allow for hash update (if hash // algorithm changed, the new hash can only be stored in the // database when we have the actual password, and only at login // time do we have the actual password) $user->updatePasswordHash($password); $authId = Auth::generateAuthId(); $auth = new Auth($authId); $auth->setUserId($userId); $auth->save(); $response = successResponse(); $response['authId'] = $authId; } else {
if (isset($_GET['userId'])) { $userId = $_GET['userId']; } else { $userId = $auth->getUserId(); if (!$userId) { $userId = ''; } } if (!isset($userId) || $userId === '') { $response = errorResponse(RESPONSE_BAD_REQUEST, 'Need user ID'); } else { if (!$auth->canGetUserBaseInfo($userId)) { $response = errorResponse(RESPONSE_UNAUTHORIZED); } else { $object = new User($userId); if ($object->getCreated() === null) { $response = errorResponse(RESPONSE_NOT_FOUND); } else { $response = successResponse(); $response['userId'] = $object->getUserId(); $response['name'] = $object->getName(); $response['deleted'] = $object->getDeleted(); if ($auth->canGetUserDetails($userId)) { $response['created'] = $object->getCreated(); $response['updated'] = $object->getUpdated(); $response['externalType'] = $object->getExternalType(); $response['externalId'] = $object->getExternalId(); $response['access'] = $object->getAccess(); $response['email'] = $object->getEmail(); $response['notification'] = $object->getNotification(); $response['tempCode'] = $object->getTempCode();