You are here

permissions.test in Weather 6.5

File

tests/permissions.test
View source
<?php

/*
 *
 * Copyright © 2008-2012 Tobias Quathamer <t.quathamer@gmx.net>
 *
 * This file is part of the Drupal Weather module.
 *
 * Weather is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Weather is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Weather; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
class WeatherPermissionTestCase extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('Permission'),
      'description' => t('Tests permission and access settings for different users.'),
      'group' => t('Weather'),
    );
  }

  /**
   * Set up testing environment
   */
  function setUp() {
    parent::setUp('weather');
  }

  /**
   * Permissions of weather block
   *
   * This test requires that at least one system wide block is enabled.
   */
  function testPermissions() {

    // This user is allowed to view the system block
    $normal_user = $this
      ->drupalCreateUser(array(
      'access content',
    ));

    // This user is allowed to administer a custom weather block
    $weather_user_1 = $this
      ->drupalCreateUser(array(
      'access content',
      'administer custom weather block',
    ));

    // This user is also allowed to administer a custom weather block,
    // like weather_user_1. However, he's not allowed to edit the
    // custom block of weather_user_1. This catches bug #244087
    // (Only one permission for everyone)
    $weather_user_2 = $this
      ->drupalCreateUser(array(
      'access content',
      'administer custom weather block',
    ));

    // This user is allowed to access the weather pages
    $weather_pages_user = $this
      ->drupalCreateUser(array(
      'access content',
      'access weather pages',
    ));

    // This user may setup a system-wide weather block.
    $admin_user = $this
      ->drupalCreateUser(array(
      'access content',
      'administer custom weather block',
      'administer site configuration',
      'administer blocks',
    ));

    // Test with admin user
    $this
      ->drupalLogin($admin_user);

    // Get different pages
    $this
      ->drupalGet('node');
    $this
      ->drupalGet('user/' . $admin_user->uid . '/weather');
    $this
      ->assertText(t('My weather'));
    $this
      ->drupalGet('admin/settings/weather');
    $this
      ->assertText(t('Create new system-wide block'));

    // Enable a system-wide weather block
    $this
      ->drupalPost('admin/settings/weather/edit/-1/1', array(), t('Save configuration'));
    $this
      ->drupalGet('admin/settings/weather');
    $this
      ->assertText('Create new location in block 1');
    $edit = array(
      'weather_3[region]' => 'right',
    );
    $this
      ->drupalPost('admin/build/block', $edit, t('Save blocks'));
    $this
      ->assertText(t('Current weather'));

    // Logout current user
    $this
      ->drupalLogout();

    // Test with normal user
    $this
      ->drupalLogin($normal_user);

    // Get different pages
    $this
      ->drupalGet('node');
    $this
      ->assertText(t('Current weather'));
    $this
      ->drupalGet('user/' . $normal_user->uid);
    $this
      ->assertNoText(t('My weather'));
    $this
      ->drupalGet('user/' . $normal_user->uid . '/weather');
    $this
      ->assertText(t('Access denied'));
    $this
      ->drupalGet('admin/settings/weather');
    $this
      ->assertText(t('Access denied'));
    $this
      ->drupalGet('weather');
    $this
      ->assertText(t('Access denied'));
    $this
      ->drupalGet('weather/EDDH');
    $this
      ->assertText(t('Access denied'));

    // Logout current user
    $this
      ->drupalLogout();

    // Test with weather user
    $this
      ->drupalLogin($weather_user_1);

    // Get different pages
    $this
      ->drupalGet('node');
    $this
      ->assertText(t('Current weather'));
    $this
      ->drupalGet('user/' . $weather_user_1->uid);
    $this
      ->assertText(t('My weather'));
    $this
      ->drupalGet('user/' . $weather_user_1->uid . '/weather');
    $this
      ->assertText(t('My weather'));
    $this
      ->drupalGet('admin/settings/weather');
    $this
      ->assertText(t('Access denied'));
    $this
      ->drupalGet('weather');
    $this
      ->assertText(t('Access denied'));
    $this
      ->drupalGet('weather/EDDH');
    $this
      ->assertText(t('Access denied'));

    // Logout current user
    $this
      ->drupalLogout();

    // Test with weather user 2
    $this
      ->drupalLogin($weather_user_2);

    // Get different pages
    $this
      ->drupalGet('node');
    $this
      ->assertText(t('Current weather'));
    $this
      ->drupalGet('user/' . $weather_user_2->uid);
    $this
      ->assertText(t('My weather'));
    $this
      ->drupalGet('user/' . $weather_user_2->uid . '/weather');
    $this
      ->assertText(t('My weather'));
    $this
      ->drupalGet('admin/settings/weather');
    $this
      ->assertText(t('Access denied'));

    // Do not allow editing another user's settings, see #244087
    $this
      ->drupalGet('user/' . $weather_user_1->uid . '/weather');
    $this
      ->assertText(t('Access denied'));

    // Logout current user
    $this
      ->drupalLogout();

    // Test with weather pages
    $this
      ->drupalLogin($weather_pages_user);

    // Get different pages
    $this
      ->drupalGet('node');
    $this
      ->assertText(t('Current weather'));
    $this
      ->assertNoText(t('My weather'));
    $this
      ->drupalGet('user/' . $weather_pages_user->uid . '/weather');
    $this
      ->assertText(t('Access denied'));
    $this
      ->drupalGet('admin/settings/weather');
    $this
      ->assertText(t('Access denied'));
    $this
      ->drupalGet('weather');
    $this
      ->assertText(t('Search for a location'));
    $this
      ->drupalGet('weather/EDDV');
    $this
      ->assertText(t('Hannover'));
    $this
      ->drupalGet('weather/hannover');
    $this
      ->assertText(t('Hannover'));
    $this
      ->drupalGet('weather/toddy');
    $this
      ->assertText(t('Your search did not return any results.'));

    // Logout current user
    $this
      ->drupalLogout();
  }

}

Classes