public function create($email, $name, $surname, $password, $password2) { $user = new User($this->db); try { $user->setEmail($email); $user->setName($name); $user->setSurname($surname); $user->setHash($password, $password2); } catch (Exception $e) { $err = $e->getmessage(); } if (!isset($err)) { $email = $this->db->quote($user->getEmail()); $name = $this->db->quote($user->getName()); $surname = $this->db->quote($user->getSurname()); $hash = $user->getHash(); $query = ' INSERT INTO user (email, name, surname, hash) VALUES (' . $email . ',' . $name . ',' . $surname . ',"' . $hash . '")'; $res = $this->db->exec($query); if ($res) { $id = $this->db->lastInsertId(); if ($id) { return $this->readById($id); } else { throw new Exception('Database error'); } } else { throw new Exception('User already exist'); } } else { throw new Exception($err); } }
public function testPut() { $user = new User(); $user->setId('2'); $user->setName('Doe'); $user->setFirstname('John'); $user->setLogin('jDoe'); $user->setPassword('3131', true); $user->setMail('*****@*****.**'); $user->setAddress('9 rue de la pochette'); $user->setPhone('0143523213'); $user->setPortable('0625884536'); $user->setSubscriptionDate('2012-11-27 08:39:00'); $user->setHash('79457832847b44a73ccfeef57c03033db88cad08'); $user->setNewsletter('1'); $user->setRole('user'); $userMapper = new UserMapper(); $userMapper->setId(2); $userMapper->updateUser($user); $this->assertEquals($user, $userMapper->selectUser()); }
/** * 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()); }
public function testGetSetHash() { $user = new User(); $user->setHash('ec457d0a974c48d5685a7efa03d137dc8bbde7e3'); $this->assertEquals('ec457d0a974c48d5685a7efa03d137dc8bbde7e3', $user->getHash()); }
$object->setAccess($data['access']); } if (isset($data['email'])) { $object->setEmail($data['email']); } if (isset($data['notification'])) { $object->setNotification($data['notification']); } if (isset($data['tempCode'])) { $object->setTempCode($data['tempCode']); } if (isset($data['deleted'])) { $object->setDeleted($data['deleted']); } if (isset($data['hash'])) { $object->setHash($data['hash']); } if ($object->save()) { $response = successResponse(); } else { $response = errorResponse(RESPONSE_INTERNAL_ERROR); } } else { $response = errorResponse(RESPONSE_BAD_REQUEST, 'Need user ID'); } } else { $response = errorResponse(RESPONSE_METHOD_NOT_ALLOWED); } } } echo json_encode($response);