Codeception API TestGuy sends empty post on SubmitForm - php

First of all, sorry for my english!
I'm writing functional tests with codeception, i'm using the ZF2 module and so far all the stuff have worked great. The problem comes when i try to submit forms, the test seems to send parameters correctly, but the $_POST is always empty.
My ExempleCept.php
$I = new TestGuy($scenario);
$I->wantTo('perform actions and see result');
$I->amOnPage('/user/login');
$I->submitForm('#login', ['email' => 'teste123#teste.com.br', 'password' => '789456']);
$I->seeCurrentUrlEquals('/user/login');
vendor/bin/codeception run --debug output
* I submit form "#login",{"email":"teste123#teste.com.br","password":"789456"}
array(0) {
}
=> [Response] 200
=> [Page] http://localhost/user/login
* I see current url equals "/user/login"
=> [Uri] /user/login
=> [Method] post
=> [Parameters] {"email":"teste123#teste.com.br","password":"789456"}
=> [Response] 200
=> [Page] http://localhost/user/login
UserController::loginAction()
public function loginAction() {
$form = new LoginForm ( 'login' );
$request = $this->getRequest ();
if ($request->isPost ()) {
var_dump($_POST);
...
Note that the var_dump line on my action, is called right after " * I submit form", so the POST is coming!
So, who can help me?
Thanks!
Edited
I've tried the same test with WebGuy API (acceptance), and it worked. But, the tests must be executed by Jenkins, and WebGuy needs an http server to run, then i realy need the TestGuy tests working

Related

Get userId on database in Silex Project

I started a new project with Silex, it looks like a cool and fast Framework but I'm getting crazy with the SecurityServiceProvider trying to get the uid. I though it should be something like the getId() like Symfony but I got an error that the function doesn't exists.
PHP Fatal error: Call to undefined method Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage::getUser()
If I got the current logged user I have his information but not the Id.
$user = $app['user'];
print_r($user);
Symfony\Component\Security\Core\User\User Object (
[username:Symfony\Component\Security\Core\User\User:private] =>
myuser#mymail.com
[password:Symfony\Component\Security\Core\User\User:private] =>
5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg==
[enabled:Symfony\Component\Security\Core\User\User:private] => 1
[accountNonExpired:Symfony\Component\Security\Core\User\User:private]
=> 1 [credentialsNonExpired:Symfony\Component\Security\Core\User\User:private]
=> 1 [accountNonLocked:Symfony\Component\Security\Core\User\User:private]
=> 1 [roles:Symfony\Component\Security\Core\User\User:private] => Array ( [0] => ROLE_USER ) )
Any idea? Thanks in advance!
// Symfony 2.6+
$token = $app['security.token_storage']->getToken();
// Symfony 2.3/2.5
$token = $app['security']->getToken();
Get user info:
$user = $token->getUser();
Check $user
var_dump($user);
Ok got the solution, if anybody implements a new UserProvider, should to add the customs fields on the provider, and define them on User class in Symfony. Thanks anyway!

How do call a method inside a component in OctoberCMS?

I don't know if this is possible or if it's a complete madness but I'm trying to execute a PHP method from AJAX call using OctoberCMS Ajax Framework(I assume that this uses jQuery behind it) and is not working because I never get redirect to PayPal site. The PHP code I'm trying to get working is this one:
protected function onExecutePurchaseMethod()
{
Omnipay::gateway('PayPal_Express');
$params = [
'username' => $this->username,
'password' => $this->password,
'signature' => $this->signature,
'testMode' => $this->sandboxMode,
'amount' => Session::get('amountToReload'),
'cancelUrl' => url( 'payment/step4', "", $secure = null ),
'returnUrl' => url( 'payment/step2', "", $secure = null ),
'currency' => 'USD'
];
$response = Omnipay::purchase($params)->send();
if ($response->isSuccessful()) {
var_dump($response);
} else {
var_dump($response->getMessage());
}
}
What is happening since none redirect to PayPal is executed and page is getting stuck many times forcing me to close the browser and reopen again, no method is executed and no visible errors. It's possible to do what I'm trying to do? Is not a madness? If it's possible where is my error?
As extra info I'm using Barryvdh Laravel-omnipay package for handle Omnipay from within Laravel.
After looking briefly through the documentation, my best guess is that you're missing a required field for the purchase() method. I believe you need a card parameter (even if it's an invalid one) to get it to process.

Symfony2 functional test redirect

I am working with Symfony2 and the FOSRestBundle and I now try to write a functional test for my rest api. I want to POST a user name and display it, similar to http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html. I.e., my controller returns a View::createRedirect([...],Codes::HTTP_CREATED) instance.
Now, my test looks like:
[...]
$client = static::createClient(array('debug'=>true));
$request = $client->request('POST', '/names',
array(),
array(),
array('CONTENT_TYPE' => 'application/json'),
$expression);
$response = $client->getResponse();
\print_r($response);
$this->assertEquals($response->getStatusCode(), 201);
$response = $client->followRedirect();
\print_r($response);
When I run it, I get this output:
Symfony\Component\HttpFoundation\Response Object
(
[...]
[location] => Array
(
[0] => /names/90
)
[...]
There was 1 error:
1) MYNAME\MyBundle\Tests\Controller\RestControllerTest::testAddUser
LogicException: The request was not redirected.
Why wasn't it redirected?
Where is my mistake?
Thank you for any help!
If you want to generate a redirect, your backend should generate a 301 (“permanent”) or 302 (“temporary”) HTTP status. A status code of 201 is not really a redirect, even if you set a Location header. I think it depends on the client implementation what happens on a 201 + Location.

PHP: Verify webhook with the Podio API

I'm using the Podio PHP API, and I want to validate a webhook as done here: https://developers.podio.com/examples/webhooks
I have a test script on my server: http://qvido.se/api/podio/ValidateHook.php which contains this code:
<?php
require_once('Depend/PodioAPI.php');
require_once('Depend/config.php');
error_log("validate triggerd");
// Setup client
Podio::setup($client_id, $client_secret);
// Turn on debugging
Podio::$debug = true;
// Authenticate the application
Podio::authenticate('app', array('app_id' => MY_APP_ID, 'app_token' => 'MY_APP_TOKEN'));
switch ($_POST['type']) {
case 'hook.verify':
// Validate the webhook
PodioHook::validate($_POST['hook_id'], array('code' => $_POST['code']));
case 'item.create':
// Do something. item_id is available in $_POST['item_id']
case 'item.update':
// Do something. item_id is available in $_POST['item_id']
case 'item.delete':
// Do something. item_id is available in $_POST['item_id']
}
?>
When hitting Verfiy in the Podio UI it doesn't seem to send a $_POST request to my script. I've turned on the debug mode but nothing gets logged in the podio.log file. Instead it shows a 302 error code while trying to send the $_POST request to my script.
I don't think my script gets called at all. How can I do this?
Assuming all other variables are defined properly, there is still an error in your static Podio::authenticate() method.
Just strip the quotes from MY_APP_TOKEN:
So
Podio::authenticate('app', array('app_id' => MY_APP_ID, 'app_token' => 'MY_APP_TOKEN'));
should better be
Podio::authenticate('app', array('app_id' => MY_APP_ID, 'app_token' => MY_APP_TOKEN));
Cheers!

Zend Router overwrites post variables from form

We are using the Zend Router and it seems that its overwriting the parameters that are sent by forms. The only parameters that arrive to the controller are the params from the Url.
Does anyone know why this is happening?
Here is the config file:
; Routing config
routes.groups.route = groups/:group/:type/:idPost/:postUrl/:page
routes.groups.defaults.controller = groups
routes.groups.defaults.action = index
routes.groups.defaults.type = main
routes.groups.defaults.idPost =
routes.groups.defaults.postUrl =
routes.groups.defaults.page = 1
And the form:
<form action="<?= $this->_view->baseUrl ?>/groups/<?= $group['groupUrl'] ?>/deletepost/" method="post">
<input type="hidden" name="formUrl" value="<?=$formUrl ?> />
...
</form>
Controller:
public function deletepostAction() {
$params = $this->getRequest()->getParams();
print_r($params);
die;
}
...that outputs:
Array
(
[group] => dandy-handwriting
[type] => deletepost
[idPost] =>
[controller] => groups
[action] => index
[postUrl] =>
[idGroup] => 1
[lang] => en
)
note that "formUrl" is missing, its only the parameters from the router.
You can use the request-object in your controller to access your data.
Fetch the request object:
$request = $this->getRequest();
Retrieve POST data (if your form is submitted via POST):
$post = $request->getPost();
Retrieve GET data (if your form is submitted via GET):
$get = $request->getQuery();
Retrieve parameter in the order user parameters set via setParam(), GET parameters and POST parameters:
$params = $request->getParams();
If you fetch your data with getParams() the params set by the router will override your POST data.
So if you only want to fetch the data from your form, use the getPost() or getQuery() method.
Probably you are sending your form data as GET, and have configured Zend_Router to rewrite the url (without taking the other get parameters).
In that case the solution is to send the form data with POST or change the routes in Zend_Router.
Your code would help to determine what your exact problem is.

Categories