<?php
namespace Drupal\user\Tests;
use Drupal\simpletest\WebTestBase;
class UserPictureTest extends WebTestBase {
public static $modules = array(
'image',
'comment',
);
protected $user;
protected $_directory_test;
public static function getInfo() {
return array(
'name' => 'User pictures',
'description' => 'Tests user picture functionality.',
'group' => 'User',
);
}
function setUp() {
parent::setUp();
$this->web_user = $this
->drupalCreateUser(array(
'access content',
'access comments',
'post comments',
'skip comment approval',
));
$this
->drupalCreateContentType(array(
'type' => 'article',
'name' => 'Article',
));
module_load_install('user');
_user_install_picture_field();
$bundle_settings = field_bundle_settings('user', 'user');
$bundle_settings['extra_fields']['display']['member_for']['compact'] = array(
'visible' => FALSE,
'weight' => 10,
);
field_bundle_settings('user', 'user', $bundle_settings);
}
function testCreateDeletePicture() {
$this
->drupalLogin($this->web_user);
$image = current($this
->drupalGetTestFiles('image'));
$file = $this
->saveUserPicture($image);
$this
->drupalGet('user');
$this
->assertRaw(file_uri_target($file->uri), 'User picture found on user account page.');
$edit = array();
$this
->drupalPost('user/' . $this->web_user->uid . '/edit', $edit, t('Remove'));
$this
->drupalPost(NULL, array(), t('Save'));
db_update('file_managed')
->fields(array(
'timestamp' => REQUEST_TIME - (DRUPAL_MAXIMUM_TEMP_FILE_AGE + 1),
))
->condition('fid', $file->fid)
->execute();
drupal_cron_run();
$this
->assertFalse(file_load($file->fid), 'File was removed from the database.');
clearstatcache(TRUE, $file->uri);
$this
->assertFalse(is_file($file->uri), 'File was removed from the file system.');
}
function testPictureOnNodeComment() {
$this
->drupalLogin($this->web_user);
$image = current($this
->drupalGetTestFiles('image'));
$file = $this
->saveUserPicture($image);
$node = $this
->drupalCreateNode(array(
'type' => 'article',
));
variable_set('theme_settings', array(
'toggle_node_user_picture' => TRUE,
));
$this
->drupalGet('node/' . $node->nid);
$this
->assertRaw(file_uri_target($file->uri), 'User picture found on node page.');
variable_set('theme_settings', array(
'toggle_comment_user_picture' => TRUE,
));
$edit = array(
'comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]' => $this
->randomString(),
);
$this
->drupalPost('comment/reply/' . $node->nid, $edit, t('Save'));
$this
->assertRaw(file_uri_target($file->uri), 'User picture found on comment.');
}
function saveUserPicture($image) {
$edit = array(
'files[user_picture_und_0]' => drupal_realpath($image->uri),
);
$this
->drupalPost('user/' . $this->web_user->uid . '/edit', $edit, t('Save'));
$account = user_load($this->web_user->uid, TRUE);
return file_load($account->user_picture[LANGUAGE_NOT_SPECIFIED][0]['fid']);
}
}