-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing Drupal pages: Help Please :-) #49
Comments
Looking at the output made me realize I have a bigger problem then just the expected one: It's assuming my site is at Furthermore, it's behind an HTAccess wall so I would need to be able to enter username/password... Could this be done in the .env file? |
We've encountered this issue before and attempted to fix it but we unfortunately couldn't find a simple solution. From what I saw on other testing frameworks for drupal 7, you have to add and activate the "drupal RESTful" module to be able to authenticate as a privileged user. I am not sure how to deal with HTTP Basic Auth (htaccess). I am sure that GuzzleHTTP (a dependency of TripalTestSuite) can handle it so I'll add that as feature request :). However, there are other limitations to HTTP Testing. Currently, here are the limitations of http testing:
We found that HTTP testing at this stage can only be useful to test unpriviledged pages that rely on data pre-existing in the database :(. I love HTTP testing in Laravel and find it very useful so we should work on finding a solution to those couple of issues. |
One way to solve both these issues is to execute the menu item's callback instead of actually calling the URL. We can then use the silent() helper method to collect output and simulate a proper HTTP response with data that's added in a transaction being available. This will also prevent any side-effects to the database from happening if we wanted to run the test in a transaction. |
I found a way 🎉 First to get around my .htaccess restriction for my test site I created a .env file like so:
Then to get around the requirement for an authenticated user // Log in the god user.
global $user;
$user = user_load(1);
// Do what you need to do
// Can't use the TripalTest get/post methods here
// have to call the functions specifically.
// @see next code block for an example.
// log out the god user.
$user = drupal_anonymous_user(); and finally, I executed the Dupal node form within the above authentication block as follows: // Create a blank node of the correct type.
$node = array('type' => 'blastdb');
// Fill in the form.
$form_state = array(
'values' => array(
'db_name' => 'Test Blast Database',
'db_path' => '/fake/path/here',
'db_dbtype' => 'nucleotide',
'dbxref_linkout_type' => 'none',
'op' => t('Save'), // CRITICAL!
),
);
// Execute the node creation form.
drupal_form_submit('blastdb_node_form', $form_state, (object) $node);
// Retrieve any errors.
$errors = form_get_errors();
$this->assertEmpty($errors); Thoughts? |
As for the rest... I think thats really interesting, I'm glad it works for you. I had been previously thinking that checking form errors/submission was better done by directly calling the method that Not sure what the advantages/disadvantages of each are. I think it is going to depend on how comlpicated the form is: for complex forms, you'll want to do it this way I bet. So again I wonder if this example should get documented.... |
IMO, this approach for uname/pass is sufficient and will be consistent across all http service providers (Apache2, Nginx...). I think form testing should always be done in this way since going through the Drupal menu will require us to obtain a CSRF token which can be hard to deal with (goes in a cookie and in the form as a hidden input). As for authentication, we had a method |
Another note, we should probably make sure to empty the |
Yes, the nice thing about calling drupal_form_submit() is that it calls all the hooks and alters to make sure you end up with the exact form and validation the user will get. A better way to authenticate a user would be much appreciated! This still feels hacky. Ideally there would be a way to authenticate the god user without hard-coding the password in the file or even needing to know what it is. |
Note: you need to add the following before drupal_form_submit() of node forms or they will fail on travis CI module_load_include('inc', 'node', 'node.pages'); |
So we added an authentication method that takes out all of the cumbersome work of authenticating and reverting to anonymous. It also manages the session state correctly as recommended by Drupal. Documentation here: https://github.com/statonlab/TripalTestSuite#user-authentication 🎊🎊🎊 |
Hi, I would like to test a specific Drupal page (Http Requests) but it requires a privileged user to be logged in. How would I go about that?
For example, I want to test the node creation form:
[mysite]/node/add/blastdb
. This is what I have so far:and this is what I get
The text was updated successfully, but these errors were encountered: