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