I have been trying to write a test to prevent regressions of #293612: user_authenticate() should work when $_COOKIE is empty. However, I've not been able to using the existing simpletest API.

The reason for this is that to reproduce the bug, cookies need to be cleared right before the POSTing of username and password, and at present, drupalPost() calls drupalGet(), which sets the CURLOPT_COOKIEJAR option of the curl handle. Even if this drupalGet() were not called, the curlExec() calls curlConnect() which sets the CURLOPT_COOKIEJAR variable.

To fix this within the framework of the API, I've introduced a $clear_cookies option to drupalPost(), curlExec() and curlConnect(). If $clear_cookies is FALSE (the default) everything works as normal.

If $clear_cookies is TRUE, the behaviour of the curlExec() called from drupalPost() is altered: curlExec() calls curlConnect() with this option, and curlConnect() then calls curlClose() to get rid of any existing cookies, and does not set CURLOPT_COOKIEJAR. curlConnect() calls curlExec() without the $clear_cookies argument, and on *this* invocation, curlExec() does not set the COOKIEJAR. However, once this curlConnect() has returned, curlExec() does set the CURLOPT_COOKIEJAR just before it invokes curl_exec().

This does seem a bit messy, but it was the only way I could see of fixing things without writing large chunks of new code. I am new to php curl, so perhaps there is a neater way of doing things. However, I couldn't see any way of unseting options, apart from curl_close().

The patch also includes an example of the use of the extra argument, in user.test. This test will throw up a failure on an unpatched drupal tree, but will pass if the patch at #293612: user_authenticate() should work when $_COOKIE is empty is applied. The existing tests in user.test all pass.

I hope this is the right queue to submit things too. Please let me know if the two parts of the patch should be separated -- I did consider doing this, but on balance it seemed better to have it all together.

Comments

dries’s picture

Might be better to change:

$options = array(), $clear_cookies = FALSE to $url_options and more generic $curl_options?

egfrith’s picture

Hmm.... I see the point, but the I'm wondering if putting $clear_cookies into $curl_options would cause confusion.

The reason for this is that at present, whenever $curl_options is mentioned in the code, it means "an array of options for curl_setopt_array()".

The $clear_cookies flag does not correspond to this usage of $curl_options. Rather, it tells curlExec() only to set CURLOPT_COOKIEJAR just before the final curl_exec().

If a $curl_options argument were provided to drupalPost(), would it be merged with the options already sent to curlExec() in drupalPost()?

chx’s picture

Status: Needs review » Reviewed & tested by the community

The original patch is good and we should not add non CURLOPT_ thing to curl_options indeed. Logging in with an empty cookies array is a problem indeed.

boombatower’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new4.57 KB

I noticed this patch no longer applied.

Re-rolled, talking with chx about documentation and while we were talking he suggested changing the variable to $curl_restart as that is more correct.

User.test no longer passes with this applied.

Logged in.	Other	user.test	112	UserRegistrationTestCase->testUserRegistration()
boombatower’s picture

Also shouldn't this change

drupalLogin() ?? which contains.

$this->drupalPost('user', $edit, t('Log in'));
egfrith’s picture

I agree, $curl_restart is a better name for the option than $clear_cookies.

I don't think the default behaviour of drupalLogin() should change, since this simulates the browser behaviour that occurs most of the time. However, it might make sense to have a $curl_restart option to drupalLogin(), which would then get passed to the drupalPost() in drupalLogin().

This would allow the new code in user.test to be shortened to something like:

// Login user without cookies
$this->drupalLogin('user', TRUE);
$this->assertText(t('Log out'), t('Logged in.'));

This thought did cross my mind when writing the original patch, but I dismissed it because as far as I can see, logging in without cookies only needs to be tested once. However, the number of lines of code with the argument to drupalLogin() (plus the doc string) will probably be less than or equal to the current number of lines of code, and perhaps there are some use cases which are as yet unclear... On balance, I would be minded to give drupalLogin() the new argument, but it's only a marginal preference.

Let me know if you'd like me to roll this into a patch.

boombatower’s picture

Seems like a good idea, never know if someone may want it later and seems cleaner than making the exception in the user.test.

egfrith’s picture

Hmm... I've looked at user.test, and noticed that the original code at line 87 (on which I modelled the new code) is:

 // Login user.
    $edit = array();
    $edit['name'] = $user->name;
    $edit['pass'] = $new_pass;
    $this->drupalPost('user', $edit, t('Log in'));

So the drupalLogin() function isn't used there. Before changing this, I'd like to know that there isn't a good reason that it's this way. And as I'd rather have both login snippets consistent, I'll hold off patching for now.

boombatower’s picture

The test is old and most likely hasn't been updated. As I helped add things like the login method.

egfrith’s picture

OK, if it's felt that updating user.test along those lines doesn't count as scope creep, I'll try to update the patch later on today.

boombatower’s picture

Your implementing the new feature your adding. I don't see that as scope creep.

egfrith’s picture

StatusFileSize
new6.58 KB

Here's the latest version of the patch. Changes from the previous version
* drupalLogin() now has a $curl_restart parameter, documented in the same way as the other functions with the parameter
* user.test now uses drupalLogin() for the test of logging in with the new password and the test of logging in without cookies. This requires setting $user->pass_raw explicitly

I've tested hat the new test causes User.test to fail when it's applied.

boombatower’s picture

Status: Needs review » Needs work

Test doesn't pass and there is a single line of extra white-space in user.test.

Found name: simpletest_CODL	User login	user.test	108	UserRegistrationTestCase->testUserRegistration()	
Logged in.	Other	user.test	109	UserRegistrationTestCase->testUserRegistration()	
egfrith’s picture

Just to be clear, when you test the patch, does is cause a test to be failed when the patch at #293612: user_authenticate() should work when $_COOKIE is empty is applied? With this patch applied, the latest patch in this thread passed all its tests, as it should. The patch in this thread is designed to cause a failure in the current version of HEAD.

I've not access to my testing rig at the moment, so I can't tell which of the blank lines in the patch is the wrong one. Will try to look at that this evening.

boombatower’s picture

Status: Needs work » Postponed

Due to no failing test policy this will need to be postponed until that patch makes it in. The spacing issue is no big deal. I would have re-rolled myself, but I thought this needed to be fixed in addition.

egfrith’s picture

StatusFileSize
new6.13 KB

The issue that this test should prevent regressions to (#293612: user_authenticate() should work when $_COOKIE is empty) is now fixed, so I have rerolled the patch against HEAD and checked that all of the user tests pass.

egfrith’s picture

StatusFileSize
new6.18 KB

Just realised I hadn't rolled the patch correctly due to running cvs diff in a subdir. Corrected patch attached.

cwgordon7’s picture

Status: Postponed » Needs review

The issue in question has been marked fixed.

Status: Needs review » Needs work

The last submitted patch failed testing.

egfrith’s picture

Status: Needs work » Needs review
StatusFileSize
new6.29 KB

Here's a new version of the patch. Apart from reworking the code, I think I've addressed the concerns of comment #13, apart from possibly the white space in user.test. There is extra white space in user.test, but I think this matches the pattern of whitespace already in user.test.

Status: Needs review » Needs work

The last submitted patch failed testing.

grendzy’s picture

I tend to agree with #1 that more generic access to curl options would be beneficial. I'm also trying to find a way to access and manipulate cookies with cURL, see #457804: Enable simpletest to manipulate cookies.

boombatower’s picture

Status: Needs work » Closed (fixed)

Believe this is now supported, reopen for 8.x if not.

JeremyFrench’s picture

Version: 7.x-dev » 8.x-dev
Status: Closed (fixed) » Needs work

I can't see anything which directly corresponds to this here http://api.drupal.org/api/drupal/core--modules--simpletest--drupal_web_t... or in D7 so I don't believe that this issue is fixed. If there as a way to do this with all of the new options it doesn't seem to be documented.

sun’s picture

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

Status: Needs work » Fixed

Mink or pure guzzle HTTP calls both enable this usecase now.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.