123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- <?php
- class ProfileTestCase extends DrupalWebTestCase {
- protected $admin_user;
- protected $normal_user;
- function setUp() {
- parent::setUp('profile');
- variable_set('user_register', USER_REGISTER_VISITORS);
- $this->admin_user = $this->drupalCreateUser(array('administer users', 'access user profiles', 'administer blocks'));
-
- $this->normal_user = $this->drupalCreateUser();
- }
-
- function createProfileField($type = 'textfield', $category = 'simpletest', $edit = array()) {
- $edit['title'] = $title = $this->randomName(8);
- $edit['name'] = $form_name = 'profile_' . $title;
- $edit['category'] = $category;
- $edit['explanation'] = $this->randomName(50);
- $this->drupalPost('admin/config/people/profile/add/' . $type, $edit, t('Save field'));
- $fid = db_query("SELECT fid FROM {profile_field} WHERE title = :title", array(':title' => $title))->fetchField();
- $this->assertTrue($fid, 'New Profile field has been entered in the database');
-
- $this->drupalGet('user/' . $this->admin_user->uid . '/edit/' . $category);
-
- if ($type == 'date') {
- $this->assertField($form_name . '[month]', 'Found month selection field');
- $this->assertField($form_name . '[day]', 'Found day selection field');
- $this->assertField($form_name . '[year]', 'Found day selection field');
- }
- else {
- $this->assertField($form_name , format_string('Found form named @name', array('@name' => $form_name)));
- }
-
- $this->assertText($title, format_string('Checking title for field %title', array('%title' => $title)));
-
- $this->assertText($edit['explanation'], format_string('Checking explanation for field %title', array('%title' => $title)));
- return array(
- 'fid' => $fid,
- 'type' => $type,
- 'form_name' => $form_name,
- 'title' => $title,
- 'category' => $category,
- );
- }
-
- function updateProfileField($fid, $type = 'textfield', $edit = array()) {
- $form_name = $edit['name'];
- $title = $edit['title'];
- $category = $edit['category'];
- $this->drupalPost('admin/config/people/profile/edit/' . $fid, $edit, t('Save field'));
-
- $this->drupalGet('user/' . $this->admin_user->uid . '/edit/' . $category);
-
- if ($type == 'date') {
- $this->assertField($form_name . '[month]', 'Found month selection field');
- $this->assertField($form_name . '[day]', 'Found day selection field');
- $this->assertField($form_name . '[year]', 'Found day selection field');
- }
- else {
- $this->assertField($form_name , format_string('Found form named @name', array('@name' => $form_name)));
- }
-
- $this->assertText($title, format_string('Checking title for field %title', array('%title' => $title)));
-
- $this->assertText($edit['explanation'], format_string('Checking explanation for field %title', array('%title' => $title)));
- return array(
- 'fid' => $fid,
- 'type' => $type,
- 'form_name' => $form_name,
- 'title' => $title,
- 'category' => $category,
- );
- }
-
- function setProfileField($field, $value = NULL) {
- if (!isset($value)) {
- $value = $this->randomName();
- }
- $edit = array(
- $field['form_name'] => $value,
- );
- $this->drupalPost('user/' . $this->normal_user->uid . '/edit/' . $field['category'], $edit, t('Save'));
-
- $content = $this->drupalGet('user/' . $this->normal_user->uid);
- $this->assertText($field['title'], format_string('Found profile field with title %title', array('%title' => $field['title'])));
- if ($field['type'] != 'checkbox') {
-
- $this->assertText("$value", format_string('Found profile field with value %value', array('%value' => $value)));
- }
- return $value;
- }
-
- function deleteProfileField($field) {
- $this->drupalPost('admin/config/people/profile/delete/' . $field['fid'], array(), t('Delete'));
- $this->drupalGet('admin/config/people/profile');
- $this->assertNoText($field['title'], format_string('Checking deleted field %title', array('%title' => $field['title'])));
- }
- }
- class ProfileTestFields extends ProfileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Test single fields',
- 'description' => 'Testing profile module with add/edit/delete textfield, textarea, list, checkbox, and url fields into profile page',
- 'group' => 'Profile'
- );
- }
-
- function testProfileFields() {
- $this->drupalLogin($this->admin_user);
-
- $field_types = array(
- 'textfield' => $this->randomName(),
- 'textarea' => $this->randomName(),
- 'list' => $this->randomName(),
- 'checkbox' => 1,
-
-
- 'url' => 'http://www.' . str_replace('_', '', $this->randomName(10)) . '.org',
- );
-
-
- foreach ($field_types as $type => $value) {
- $field = $this->createProfileField($type);
- $this->setProfileField($field, $value);
- $edit = array(
- 'name' => $field['form_name'],
- 'title' => $this->randomName(),
- 'category' => $field['category'],
- 'explanation' => $this->randomName(),
- );
- $field = $this->updateProfileField($field['fid'], $field['type'], $edit);
- $this->deleteProfileField($field);
- }
- }
- }
- class ProfileTestSelect extends ProfileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Test select field',
- 'description' => 'Testing profile module with add/edit/delete a select field',
- 'group' => 'Profile'
- );
- }
-
- function testProfileSelectionField() {
- $this->drupalLogin($this->admin_user);
- $edit = array(
- 'options' => implode("\n", range(1, 10)),
- );
- $field = $this->createProfileField('selection', 'simpletest', $edit);
- $this->setProfileField($field, rand(1, 10));
- $edit = array(
- 'name' => $field['form_name'],
- 'title' => $this->randomName(),
- 'category' => $field['category'],
- 'explanation' => $this->randomName(),
- );
- $field = $this->updateProfileField($field['fid'], $field['type'], $edit);
- $this->deleteProfileField($field);
- }
- }
- class ProfileTestDate extends ProfileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Test date field',
- 'description' => 'Testing profile module with add/edit/delete a date field',
- 'group' => 'Profile'
- );
- }
-
- function testProfileDateField() {
- $this->drupalLogin($this->admin_user);
- variable_set('date_format_short', 'm/d/Y - H:i');
- $field = $this->createProfileField('date');
-
- $edit = array(
- $field['form_name'] . '[month]' => 1,
- $field['form_name'] . '[day]' => 9,
- $field['form_name'] . '[year]' => 1983,
- );
- $this->drupalPost('user/' . $this->normal_user->uid . '/edit/' . $field['category'], $edit, t('Save'));
-
- $this->drupalGet('user/' . $this->normal_user->uid);
- $this->assertText($field['title'], format_string('Found profile field with title %title', array('%title' => $field['title'])));
- $this->assertText('01/09/1983', 'Found date profile field.');
- $edit = array(
- 'name' => $field['form_name'],
- 'title' => $this->randomName(),
- 'category' => $field['category'],
- 'explanation' => $this->randomName(),
- );
- $field = $this->updateProfileField($field['fid'], $field['type'], $edit);
- $this->deleteProfileField($field);
- }
- }
- class ProfileTestWeights extends ProfileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Test field weights',
- 'description' => 'Testing profile modules weigting of fields',
- 'group' => 'Profile'
- );
- }
- function testProfileFieldWeights() {
- $this->drupalLogin($this->admin_user);
- $category = $this->randomName();
- $field1 = $this->createProfileField('textfield', $category, array('weight' => 1));
- $field2 = $this->createProfileField('textfield', $category, array('weight' => -1));
- $this->setProfileField($field1, $this->randomName(8));
- $this->setProfileField($field2, $this->randomName(8));
- $profile_edit = $this->drupalGet('user/' . $this->normal_user->uid . '/edit/' . $category);
- $this->assertTrue(strpos($profile_edit, $field1['title']) > strpos($profile_edit, $field2['title']), 'Profile field weights are respected on the user edit form.');
- $profile_page = $this->drupalGet('user/' . $this->normal_user->uid);
- $this->assertTrue(strpos($profile_page, $field1['title']) > strpos($profile_page, $field2['title']), 'Profile field weights are respected on the user profile page.');
- }
- }
- class ProfileTestAutocomplete extends ProfileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Autocompletion',
- 'description' => 'Test profile fields with autocompletion.',
- 'group' => 'Profile'
- );
- }
-
- function testAutocomplete() {
- $this->drupalLogin($this->admin_user);
-
- $category = $this->randomName();
- $field = $this->createProfileField('textfield', $category, array('weight' => 1, 'autocomplete' => 1));
-
- $field['value'] = $this->randomName();
- $this->setProfileField($field, $field['value']);
-
-
- $current_clean_url = isset($GLOBALS['conf']['clean_url']) ? $GLOBALS['conf']['clean_url'] : NULL;
- $GLOBALS['conf']['clean_url'] = 0;
- $autocomplete_url = url('profile/autocomplete/' . $field['fid'], array('absolute' => TRUE, 'script' => 'index.php'));
- $GLOBALS['conf']['clean_url'] = $current_clean_url;
- $autocomplete_id = drupal_html_id('edit-' . $field['form_name'] . '-autocomplete');
- $autocomplete_html = '<input type="hidden" id="' . $autocomplete_id . '" value="' . $autocomplete_url . '" disabled="disabled" class="autocomplete" />';
-
- $this->drupalGet('user/' . $this->admin_user->uid . '/edit/' . $category);
- $this->assertRaw($autocomplete_html, 'Autocomplete found.');
- $this->assertFieldByXPath(
- '//input[@type="text" and @name="' . $field['form_name'] . '" and contains(@class, "form-autocomplete")]',
- '',
- 'Text input field found'
- );
- $this->assertRaw('misc/autocomplete.js', 'Autocomplete JavaScript found.');
- $this->assertRaw('class="form-text form-autocomplete"', 'Autocomplete form element class found.');
-
-
- $this->drupalGet('profile/autocomplete/' . $field['fid'] . '/' . $field['value'][0]);
- $this->assertResponse(200, 'Autocomplete path allowed to user with permission.');
- $this->assertRaw($field['value'], 'Autocomplete value found.');
-
- $this->drupalLogout();
- $this->drupalLogin($this->normal_user);
-
- $this->drupalGet('user/' . $this->normal_user->uid . '/edit/' . $category);
- $this->assertNoRaw($autocomplete_html, 'Autocomplete not found.');
-
- $this->drupalGet('profile/autocomplete/' . $field['fid'] . '/' . $field['value'][0]);
- $this->assertResponse(403, 'Autocomplete path denied to user without permission.');
- }
- }
- class ProfileBlockTestCase extends ProfileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Block availability',
- 'description' => 'Check if the Author Information block is available.',
- 'group' => 'Profile',
- );
- }
- function setUp() {
- parent::setUp();
-
- $this->drupalLogin($this->admin_user);
-
- $category = $this->randomName();
- $this->field1 = $this->createProfileField('textfield', $category, array('weight' => 0));
- $this->field2 = $this->createProfileField('textfield', $category, array('weight' => 1));
-
- $this->value1 = $this->setProfileField($this->field1);
- $this->value2 = $this->setProfileField($this->field2);
-
- $this->node = $this->drupalCreateNode(array(
- 'uid' => $this->normal_user->uid,
- ));
- }
- function testAuthorInformationBlock() {
-
- $edit = array();
- $edit['blocks[profile_author-information][region]'] = 'footer';
- $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
- $this->assertText(t('The block settings have been updated.'), 'Block successfully move to footer region.');
-
- $this->drupalPost('admin/structure/block/manage/profile/author-information/configure', array(
- 'profile_block_author_fields[' . $this->field1['form_name'] . ']' => TRUE,
- ), t('Save block'));
- $this->assertText(t('The block configuration has been saved.'), 'Block configuration set.');
-
- $this->drupalGet('node/' . $this->node->nid);
- $this->assertRaw($this->value1, 'Field 1 is displayed');
- $this->assertNoRaw($this->value2, 'Field 2 is not displayed');
-
- $this->drupalPost('admin/structure/block/manage/profile/author-information/configure', array(
- 'profile_block_author_fields[' . $this->field1['form_name'] . ']' => FALSE,
- 'profile_block_author_fields[' . $this->field2['form_name'] . ']' => TRUE,
- ), t('Save block'));
- $this->assertText(t('The block configuration has been saved.'), 'Block configuration set.');
-
- $this->drupalGet('node/' . $this->node->nid);
- $this->assertNoRaw($this->value1, 'Field 1 is not displayed');
- $this->assertRaw($this->value2, 'Field 2 is displayed');
-
- $this->drupalPost('admin/structure/block/manage/profile/author-information/configure', array(
- 'profile_block_author_fields[' . $this->field1['form_name'] . ']' => TRUE,
- 'profile_block_author_fields[' . $this->field2['form_name'] . ']' => TRUE,
- ), t('Save block'));
- $this->assertText(t('The block configuration has been saved.'), 'Block configuration set.');
-
- $this->drupalGet('node/' . $this->node->nid);
- $this->assertRaw($this->value1, 'Field 1 is displayed');
- $this->assertRaw($this->value2, 'Field 2 is displayed');
-
- $this->drupalPost('admin/structure/block/manage/profile/author-information/configure', array(
- 'profile_block_author_fields[user_profile]' => TRUE,
- ), t('Save block'));
- $this->assertText(t('The block configuration has been saved.'), 'Block configuration set.');
-
- $this->drupalGet('node/' . $this->node->nid);
- $this->clickLink(t('View full user profile'));
- $this->assertEqual($this->getUrl(), url('user/' . $this->normal_user->uid, array('absolute' => TRUE)));
- }
- }
- class ProfileTestBrowsing extends ProfileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Profile browsing',
- 'description' => 'Test profile browsing.',
- 'group' => 'Profile',
- );
- }
-
- function testProfileBrowsing() {
- $this->drupalLogin($this->admin_user);
- $field = $this->createProfileField('list', 'simpletest', array('page' => '%value'));
-
- $value = $this->setProfileField($field);
-
- $this->drupalGet("profile/{$field['form_name']}/$value");
- $this->assertText($this->normal_user->name);
- }
- }
- class ProfileCrudTestCase extends ProfileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Profile CRUD tests',
- 'description' => 'Test profile integration with user create, read, update, delete.',
- 'group' => 'Profile',
- );
- }
-
- public function testUserCRUD() {
-
- $edit = array(
- 'name' => 'Test user',
- 'mail' => 'test@example.com',
- );
-
-
- $account = user_save(NULL, $edit);
-
-
- $account = user_load($account->uid);
-
-
- $account = user_save($account, $edit);
-
-
- user_delete($account->uid);
- }
- }
-
|