2

I have 1 function that returns value of any field of particular content type. For this I need to load my current user and a node from where I will retrieve value.

But loading user calls User class and when I run the test, it says undefined method - user_load.

My function is:

public static function get_ctype_field_value($field) {

    $user = user_load(\Drupal::currentUser()->id());
    $ulm = get_type_id($user); // another function in some class, return value 445
    $local_market = \Drupal\node\Entity\Node::load($ulm);
    $llf = '';
    if ($local_market) {
      // Get the field value from the LM.
      if ($local_market->hasField($field)) {
        $localmarket_field = $local_market->$field->value;
      }
    }
    return $localmarket_field;
  }

AND my test function is:

class MyControllerTest extends UnitTestCase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['node'];

  /**
   * Users to use during this test.
   *
   * @var array
   */
  protected $users = [];

  /**
   * Nodes to use during this test.
   *
   * @var array
   */
  protected $nodes = [];

  protected function setUp() {
    parent::setUp();
    $container = new ContainerBuilder();
    $methods = get_class_methods('\Drupal\Core\Entity\EntityManager');

    $entityManager = $this->getMockBuilder('\Drupal\Core\Entity\EntityManager')
  ->disableOriginalConstructor()
  ->getMock();

  }

  public function test_get_ctype_field_value() {
    $originalString = 'field_vat';
    $expectedResult = '10.00';
    $econtroller = new myController(); //controller where function resides
    $this->account->expects($this->once())
      ->method('user_load');
    $this->users[] = $this->account->load(1);
    $result = $econtroller->get_ctype_field_value($originalString);
    $this->assertEquals($expectedResult, $result);
  }
}

Please suggest any way to write PHPUnit test for this function.

The error I'm getting is:

Error: Call to a member function load() on null

1
  • I think this question is Drupal-related because you can mock the container based on Drupal core classes.
    – mradcliffe
    Commented Nov 16, 2017 at 15:37

1 Answer 1

2

The code is dependent on Drupal's service container existing, and thus a container needs to be instantiated and used. This is possible in PHPUnit tests using the \Drupal::setContainer() method, and creating a container object.

use Drupal\Core\DependencyInjection\ContainerBuilder;

protected function setUp() {
  parent::setUp();

  $this->account = $this
    ->getMockBuilder('Drupal\user\Entity\User')
    ->disableOriginalConstructor()
    ->getMock();

  // Create a new container object.
  $container = new ContainerBuilder();

  // Add a service into it.
  $container->set('current_user', $this->account);

  // Let Drupal use your mock container.
  \Drupal::setContainer($container);
}
5
  • After running this tests, it gives me error Error: Call to undefined function Drupal\mymodule\Controller\user_load() Commented Nov 17, 2017 at 0:23
  • Also getting 1 more error : Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "entity.manager". Commented Nov 17, 2017 at 0:32
  • 1
    You need to mock the entity.manager service for this to work, making it return a mocked entity storage that returns the mocked user. Also, user_load() is undefined because the user.module file is not included - you should probably be using User::load() here. Commented Nov 20, 2017 at 11:01
  • Feel free to expand on my answer with your own. I probably won't have enough time to edit mine. I missed the user_load in the question.
    – mradcliffe
    Commented Nov 20, 2017 at 20:26
  • i can't change the original function. So I have to use user_load only. Is there any other way to test these deprecated functions. Because modification of actual function is out of scope for me. Commented Nov 22, 2017 at 7:18

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.