View source
<?php
namespace Drupal\profile\Tests;
use Drupal\simpletest\WebTestBase;
class ProfileFieldAccessTest extends WebTestBase {
public static $modules = array(
'profile',
'text',
'field_ui',
);
private $type;
private $admin_user;
private $web_user;
private $other_user;
function setUp() {
parent::setUp();
$this->type = entity_create('profile_type', array(
'id' => 'personal',
'label' => 'Personal data',
'weight' => 0,
'registration' => TRUE,
));
$this->type
->save();
$this
->checkPermissions(array(), TRUE);
$this->admin_user = $this
->drupalCreateUser(array(
'access user profiles',
'administer profile types',
'administer profile fields',
'administer profile display',
'bypass profile access',
));
$user_permissions = array(
'access user profiles',
'add own personal profile',
'edit own personal profile',
'view any personal profile',
);
$this->web_user = $this
->drupalCreateUser($user_permissions);
$this->other_user = $this
->drupalCreateUser($user_permissions);
}
function testPrivateField() {
$id = $this->type
->id();
$this
->drupalLogin($this->admin_user);
$edit = array(
'new_storage_type' => 'string',
'label' => 'Secret',
'field_name' => 'secret',
);
$this
->drupalPostForm("admin/config/people/profiles/types/manage/{$id}/fields/add-field", $edit, t('Save and continue'));
$edit = array(
'field[settings][profile_private]' => 1,
);
$this
->drupalPostForm(NULL, $edit, t('Save field settings'));
$this
->drupalPostForm(NULL, array(), t('Save settings'));
$this
->drupalLogin($this->web_user);
$uid = $this->web_user
->id();
$secret = $this
->randomMachineName();
$edit = array(
'field_secret[0][value]' => $secret,
);
$this
->drupalPostForm("user/{$uid}/edit/profile/{$id}", $edit, t('Save'));
drupal_flush_all_caches();
$this
->drupalGet("user/{$uid}");
$this
->assertText($secret);
$this
->drupalLogin($this->admin_user);
$this
->drupalGet("user/{$uid}");
$this
->assertText($secret);
$this
->drupalLogin($this->other_user);
$this
->drupalGet("user/{$uid}");
$this
->assertNoText($secret);
}
}