Skip to content
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

dev/drupal#127 - require email for the Useradd task and errors not showing #17915

Merged
merged 1 commit into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CRM/Contact/Form/Task/Useradd.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function buildQuickForm() {
$this->addRule('cms_pass', 'Password is required', 'required');
$this->addRule(['cms_pass', 'cms_confirm_pass'], 'ERROR: Password mismatch', 'compare');
$this->add('text', 'email', ts('Email:'), ['class' => 'huge'])->freeze();
$this->addRule('email', 'Email is required', 'required');
$this->add('hidden', 'contactID');

//add a rule to check username uniqueness
Expand Down Expand Up @@ -101,8 +102,12 @@ public function postProcess() {
// store the submitted values in an array
$params = $this->exportValues();

CRM_Core_BAO_CMSUser::create($params, 'email');
CRM_Core_Session::setStatus('', ts('User Added'), 'success');
if (CRM_Core_BAO_CMSUser::create($params, 'email') === FALSE) {
CRM_Core_Error::statusBounce(ts('Error creating CMS user account.'));
}
else {
CRM_Core_Session::setStatus(ts('User Added'), '', 'success');
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions CRM/Utils/System/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public function createUser(&$params, $mail) {
// Validate the user object
$violations = $account->validate();
if (count($violations)) {
foreach ($violations as $violation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@demeritcowboy do we need to do this on other CMSes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For drupal 7 no, because of the way it uses drupal's form layer to create a user, so the form errors display.

Let's take a look at what wordpress/joomla do...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok it took a minute to find but it looks like wordpress doesn't override checkPermissionAddUser() so it returns false so you don't even see this feature.

Ok now joomla...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Joomla UF class has pretty extensive checking as a form rule via checkUserNameEmailExists(), and it's beyond my joomla knowledge to know how to get past it to trigger a fail in joomla itself. The only thing I can see it doesn't check for is a blank email, which this PR would cover. But createUser itself doesn't have any kind of error checking, and in fact seems like it would never return FALSE either way, so technically createUser might need some sprucing up, but I'm not a joomla person.

CRM_Core_Session::setStatus($violation->getPropertyPath() . ': ' . $violation->getMessage(), '', 'alert');
}
return FALSE;
}

Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/CRM/Contact/Form/Task/UseraddTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* @group headless
*/
class CRM_Contact_Form_Task_UseraddTest extends CiviUnitTestCase {

/**
* Test postProcess failure.
*
* In unit tests, the CMS user creation will always fail, but that's
* ok because that's what we're testing here.
*/
public function testUserCreateFail() {
$form = new CRM_Contact_Form_Task_Useradd();
// We don't need to set params or anything because we're testing fail,
// which the user creation will do in unit tests no matter what we set.
// But before the patch, the status messages were always success no
// matter what.
try {
$form->postProcess();
}
catch (CRM_Core_Exception_PrematureExitException $e) {
}
$statuses = CRM_Core_Session::singleton()->getStatus(TRUE);
$this->assertEquals('alert', $statuses[0]['type']);
}

}