public function createAction() { $this->allow_admin(); $user = $this->em->getRepository('ZfcUser\\Entity\\User')->findBy(array('email' => $_GET['user']['email'])); if (!$user) { $options = $this->getServiceLocator()->get('zfcuser_module_options'); $this->bcrypt = new Bcrypt(); $this->bcrypt->setCost($options->getPasswordCost()); $cryptPassword = $this->bcrypt->create($_GET['user']['password']); $user = new User(); $user->setUsername($_GET['user']['username']); $user->setEmail($_GET['user']['email']); $user->setPassword($cryptPassword); $this->em->persist($user); $this->em->flush(); $profile = new Profile(); $profile->setEmail($_GET['user']['email']); $profile->setResource($_GET['profile']['type']); $this->em->persist($profile); $this->em->flush(); $return = array("id" => $_GET['user']['email'], 'is_success' => 1, "flash" => "Usuario ha sido creado"); } else { $return = array('invalid_form' => 1, 'error_list' => array("Ya existe:" . $_GET['user']['email'])); } return new JsonModel($return); }
/** * * @dataProvider providerTestRegisterAction * @depend testActionControllHasIdentity * @depend testRegisterActionIsNotAllowed */ public function testRegisterAction($wantRedirect, $postRedirectGetReturn, $registerSuccess, $loginAfterSuccessWith) { $controller = $this->controller; $redirectUrl = 'http://localhost/redirect1'; $route_url = '/user/register'; $expectedResult = null; $this->setUpZfcUserAuthenticationPlugin(array('hasIdentity' => false)); $this->options->expects($this->any())->method('getEnableRegistration')->will($this->returnValue(true)); $request = $this->getMock('Zend\\Http\\Request'); $this->helperMakePropertyAccessable($controller, 'request', $request); $userService = $this->getMock('\\ZfcUser\\Service\\User'); $controller->setUserService($userService); $form = $this->getMockBuilder('\\Zend\\Form\\Form')->disableOriginalConstructor()->getMock(); $controller->setRegisterForm($form); $this->options->expects($this->any())->method('getUseRedirectParameterIfPresent')->will($this->returnValue((bool) $wantRedirect)); if ($wantRedirect) { $params = new Parameters(); $params->set('redirect', $redirectUrl); $request->expects($this->any())->method('getQuery')->will($this->returnValue($params)); } $url = $this->getMock('Zend\\Mvc\\Controller\\Plugin\\Url'); $url->expects($this->at(0))->method('fromRoute')->with($controller::ROUTE_REGISTER)->will($this->returnValue($route_url)); $this->pluginManagerPlugins['url'] = $url; $prg = $this->getMock('\\Zend\\Mvc\\Controller\\Plugin\\PostRedirectGet'); $this->pluginManagerPlugins['prg'] = $prg; $redirectQuery = $wantRedirect ? '?redirect=' . rawurlencode($redirectUrl) : ''; $prg->expects($this->once())->method('__invoke')->with($route_url . $redirectQuery)->will($this->returnValue($postRedirectGetReturn)); if ($registerSuccess) { $user = new UserIdentity(); $user->setEmail('*****@*****.**'); $user->setUsername('zfc-user'); $userService->expects($this->once())->method('register')->with($postRedirectGetReturn)->will($this->returnValue($user)); $userService->expects($this->any())->method('getOptions')->will($this->returnValue($this->options)); $this->options->expects($this->once())->method('getLoginAfterRegistration')->will($this->returnValue(!empty($loginAfterSuccessWith))); if ($loginAfterSuccessWith) { $this->options->expects($this->once())->method('getAuthIdentityFields')->will($this->returnValue(array($loginAfterSuccessWith))); $expectedResult = new \stdClass(); $forwardPlugin = $this->getMockBuilder('Zend\\Mvc\\Controller\\Plugin\\Forward')->disableOriginalConstructor()->getMock(); $forwardPlugin->expects($this->once())->method('dispatch')->with($controller::CONTROLLER_NAME, array('action' => 'authenticate'))->will($this->returnValue($expectedResult)); $this->pluginManagerPlugins['forward'] = $forwardPlugin; } else { $response = new Response(); $route_url = '/user/login'; $redirectUrl = isset($postRedirectGetReturn['redirect']) ? $postRedirectGetReturn['redirect'] : null; $redirectQuery = $redirectUrl ? '?redirect=' . rawurlencode($redirectUrl) : ''; $redirect = $this->getMock('Zend\\Mvc\\Controller\\Plugin\\Redirect'); $redirect->expects($this->once())->method('toUrl')->with($route_url . $redirectQuery)->will($this->returnValue($response)); $this->pluginManagerPlugins['redirect'] = $redirect; $url->expects($this->at(1))->method('fromRoute')->with($controller::ROUTE_LOGIN)->will($this->returnValue($route_url)); } } /*********************************************** * run */ $result = $controller->registerAction(); /*********************************************** * assert */ if ($postRedirectGetReturn instanceof Response) { $expectedResult = $postRedirectGetReturn; } if ($expectedResult) { $this->assertSame($expectedResult, $result); return; } if ($postRedirectGetReturn === false) { $expectedResult = array('registerForm' => $form, 'enableRegistration' => true, 'redirect' => $wantRedirect ? $redirectUrl : false); } elseif ($registerSuccess === false) { $expectedResult = array('registerForm' => $form, 'enableRegistration' => true, 'redirect' => isset($postRedirectGetReturn['redirect']) ? $postRedirectGetReturn['redirect'] : null); } if ($expectedResult) { $this->assertInternalType('array', $result); $this->assertArrayHasKey('registerForm', $result); $this->assertArrayHasKey('enableRegistration', $result); $this->assertArrayHasKey('redirect', $result); $this->assertEquals($expectedResult, $result); } else { $this->assertInstanceOf('\\Zend\\Http\\Response', $result); $this->assertSame($response, $result); } }
public function providerTestFindBy() { $user = new Entity(); $user->setEmail('*****@*****.**'); $user->setUsername('zfc-user'); $user->setDisplayName('Zfc-User'); $user->setId('1'); $user->setState(1); $user->setPassword('zfc-user'); return array(array('findByEmail', array($user->getEmail()), array('whereArgs' => array(array('email' => $user->getEmail()), 'AND')), array(), $user), array('findByUsername', array($user->getUsername()), array('whereArgs' => array(array('username' => $user->getUsername()), 'AND')), array(), $user), array('findById', array($user->getId()), array('whereArgs' => array(array('user_id' => $user->getId()), 'AND')), array(), $user)); }
public function getNewGuestUser($id) { $user = new User(); $user->setUsername('guest_' . $id)->setDisplayName('guest_' . $id); return $user; }