0

I have a number of users in my site,I have developed custom module for drupal mail functionality.

But I want to write test cases for custom module.

then here is the problem is how can I get the (db_select) users table data and write down in .test file.

for example in .module file we use like this:

$select = db_select('{users}', 'users');
$select -> addField('users', 'name', 'users_name');
$select -> addField('users', 'uid', 'uid');
$select -> addField('users', 'mail', 'users_mail');
$select -> addField('users', 'created', 'users_created');
$select -> addField('users', 'access', 'users_access');
$select -> addField('user_relationships', 'requester_id', 'requester_id');
$select -> addField('users_roles', 'rid', 'role_id');
$select -> addField('users_user_relationships', 'mail', 'users_user_relationships_mail');
$select -> leftjoin('user_relationships', 'user_relationships', "users.uid=user_relationships.requestee_id");
$select -> leftjoin('users', 'users_user_relationships', "user_relationships.requester_id=users_user_relationships.uid");
$select -> innerjoin('users_roles', 'users_roles', "users.uid = users_roles.uid");
$select -> condition('users.status', 0, '<>');
$select -> condition('users_roles.rid', array(
  '4',
  '5'
), 'IN');
$subresult = $select -> execute();

and i have wrote .test file like this

<?php
class BiTestMailsTests extends DrupalWebTestCase {
protected $privileged_user;
public static function getInfo() {
  return array(
    'name' => 'Test Mails',
    'description' => 'Tests for the mail module.',
    'group' => 'Test Mail Group',
 );
 }
public function setUp() {
   parent::setUp(array('mails'));

   }

public function testmail() {
    // Create an administrative user.
    $account = $this->drupalCreateUser(array('administer actions'));
    $this->drupalLogin($account);

      //database files
    $results= db_select('users','u')->fields('u',array('uid','name','mail','created','access'))->execute()->fetchAll();
    if($results){
     foreach($results as $res){
     $user_fields = user_load($res -> uid);
     $this->verbose('language:'.$language);
     $string= 'uid:'.$res->uid.' ,name:'.$res->name.' ,mail:'.$res->mail.',created date:'.$res->created.',Access:'.$res->access.'\n\r';
     $this->verbose($string);
    }
    }
    }
    }

but temporary user created and that values are getting. i don't want temp values,i want database USERS table values. How can I write down users information(name and email) in .test file?

1

Your just need get your select put it in FOREACH and write down in file.(something like below code can help you_

$results= db_select('users','u')->fields('u',array('uid','name','email'))->execute()->fetchAll();
if($results){
     $fh=fopen(drupal_get_path('module',   'yourcustommodulename').'/usertest.txt','w+');
     foreach($results as $res){
     $string= 'uid:'.$res->uid.' ,name:'.$res->name.' ,email:'.$res->email.'\n\r';
    fwrite($fh,$string);
  }
  fclose($fh);

}

5
  • how can i retrieve his role and put it in one array.
    – RaMeSh
    Aug 12 '15 at 4:12
  • i have edited my question,how can i write that query in .test file.
    – RaMeSh
    Aug 12 '15 at 4:22
  • @User you can add each of users table fields, you just need add it to ->fields('u',array('uid','name','email')),as example if you need Roles use ->fields('u',array('uid','name','email','roles'))
    – Yuseferi
    Aug 12 '15 at 4:35
  • but users table values are not getting,only temporary values are getting.
    – RaMeSh
    Aug 12 '15 at 5:35
  • i have edited my question,please tell me some sollutions for that.
    – RaMeSh
    Aug 12 '15 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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