7

My Behat test starts like this:

@api
Scenario: Testing user
Given users:
    | name | pass | mail    | status | roles  | field_lines_ref |
    | test | test | [email protected] | 1      | Tester | Line Name       |

field_lines_ref is an entity reference to a node. The user gets created but the Lines Reference field never gets populated. I also tried using the node id but it also didn't work.

One thing to note is that I already have a node created "Line Name" and I have to use that exact node because of logic in place (Based on node id).

3
  • I don't know how helpful you find it for me to say so, but this works for me with an entity reference on a node, and other node content. When you say "it didn't work", does it fail silently? Do you get an error of the form No entity 'Line Name' of type 'node' exists. (Exception)? More info might help.
    – J-P
    Commented Mar 28, 2017 at 14:50
  • I don't get an error. It just doesn't add the value.
    – albertski
    Commented Mar 28, 2017 at 17:04
  • That's the behaviour when field_lines_ref simply doesn't exist as a field (among other possible problems.) What Drupal major version are you using? I only ask because: could field_lines_ref actually be a field on a separate profile entity? (It's also possible that the extension drivers just don't support fields on users; what api_driver are you using? Try "drupal" rather than "drush".)
    – J-P
    Commented Mar 30, 2017 at 8:17

2 Answers 2

1

If you have an entity reference field, you need to refer to the entity with it's name/title/label field (this applies for taxonomy terms too). Like you did it.

This code will run for your association:

/**
 * Entityreference field handler for Drupal 7.
 */
class EntityreferenceHandler extends AbstractHandler {

  /**
   * {@inheritdoc}
   */
  public function expand($values) {
    $entity_type = $this->fieldInfo['settings']['target_type'];
    $entity_info = entity_get_info($entity_type);
    // For users set label to username.
    if ($entity_type == 'user') {
      $entity_info['entity keys']['label'] = 'name';
    }

    $return = array();
    foreach ($values as $value) {
      $target_id = db_select($entity_info['base table'], 't')
        ->fields('t', array($entity_info['entity keys']['id']))
        ->condition('t.' . $entity_info['entity keys']['label'], $value)
        ->execute()->fetchField();
      if ($target_id) {
        $return[$this->language][] = array('target_id' => $target_id);
      }
    }
    return $return;
  }

}

Which also means, the first item will be tried to associate with your user, which doesn't mean, that it can be (i.e. for content type restriction). So make sure you have only one entity which named like you want to associate. Also make sure, you don't have ', ' and ' - ' in your value, because those will be converted to multi value, and if ' - ' is present than the ': ' value will be converted to key: value pairs. You need to say exactly the entity name which is possible to refer inside that field (but as far as I can see in the code, make sure you don't have space before and/or after the name field's value, because the query above may fail to find your entity). Also check if your field is a real field, (not like the path 'field', which is not a field) you can do it, be executing the field_info_field_map() command, and if you find your field machine name there, as a key, you are fine.

If all the above is fulfilled and you already has a node which can be referred by the user's field_lines_ref field and the node title is 'Line Name' you should be able to create your user with the reference:

Given users:
    | name | pass | mail    | status | roles  | field_lines_ref |
    | test | test | [email protected] | 1      | Tester | Line Name       |

From features file or

$user_obj = (object) array(
  'name' => 'test',
  'pass' => 'test',
  'mail' => '[email protected]',
  'status' => '1',
  'roles' => 'Tester',
  'field_lines_ref' => 'Line Name',
);
$user = $this->userCreate($user_obj);

from context object.

If these doesn't help, you may need to debug your own code (including vendor code), to see where is the problem.

0

Create a 'Given' function and other supporting functions in your Context.php file along this line...

/**
 * @Transform :user
 */
public function castUsernameToUser($user) {
    $user_loaded = user_load_by_name ( $user );
    if (!$user_loaded) {
        throw new \Exception('User was not found: ' . $user);
    }
    return $user_loaded;
}

/**
 * @Transform :field_line
 */
public function castFieldLineToNode($field_line) {
    //load your node using the name 
    return $node;
}

/**
 * @Given :user has field line :field_line
 */
public function userHasFieldLine($user, $field_line) {
    $user->set ( 'field_lines_ref', $field_line);
    $user->save ();
}

Then after you create the user, add a step to your feature file like:

And "test" has field line "Line Name"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.