environment_test.rb 12.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
require File.dirname(__FILE__) + '/../test_helper'

class EnvironmentTest < Test::Unit::TestCase
  fixtures :environments

  def test_exists_default_and_it_is_unique
    Environment.delete_all
    vc = Environment.new(:name => 'Test Community')
    vc.is_default = true
    assert vc.save

    vc2 = Environment.new(:name => 'Another Test Community')
    vc2.is_default = true
    assert !vc2.valid?
    assert vc2.errors.invalid?(:is_default)

    assert_equal vc, Environment.default
  end

  def test_acts_as_configurable
    vc = Environment.new(:name => 'Testing Environment')
    assert_kind_of Hash, vc.settings
    vc.settings[:some_setting] = 1
    assert vc.save
    assert_equal 1, vc.settings[:some_setting]
  end

  def test_available_features
    assert_kind_of Hash, Environment.available_features
  end

  def test_mock
    assert_equal ['feature1', 'feature2', 'feature3'], Environment.available_features.keys.sort
  end

  def test_features
    v = Environment.new
    v.enable('feature1')
    assert v.enabled?('feature1')
    v.disable('feature1')
    assert !v.enabled?('feature1')
  end

  def test_enabled_features
    v = Environment.new
    v.enabled_features = [ 'feature1', 'feature2' ]
    assert v.enabled?('feature1') && v.enabled?('feature2') && !v.enabled?('feature3')
  end

  def test_enabled_features_no_features_enabled
    v = Environment.new
    v.enabled_features = nil
    assert !v.enabled?('feature1') && !v.enabled?('feature2') && !v.enabled?('feature3')
  end

  def test_name_is_mandatory
    v = Environment.new
    v.valid?
    assert v.errors.invalid?(:name)
    v.name = 'blablabla'
    v.valid?
    assert !v.errors.invalid?(:name)
  end

  def test_terms_of_use
    v = Environment.new(:name => 'My test environment')
    assert_nil v.terms_of_use
    v.terms_of_use = 'To be part of this environment, you must accept the following terms: ...'
    assert v.save
    id = v.id
    assert_equal 'To be part of this environment, you must accept the following terms: ...', Environment.find(id).terms_of_use
  end

  def test_has_terms_of_use
    v = Environment.new
    assert !v.has_terms_of_use?
    v.terms_of_use = 'some terms of use'
    assert v.has_terms_of_use?
  end

  def test_terms_of_enterprise_use
    v = Environment.new(:name => 'My test environment')
    assert_nil v.terms_of_enterprise_use
    v.terms_of_enterprise_use = 'To be owner of an enterprise in this environment, you must accept the following terms: ...'
    assert v.save
    id = v.id
    assert_equal 'To be owner of an enterprise in this environment, you must accept the following terms: ...', Environment.find(id).terms_of_enterprise_use
  end

  def test_has_terms_of_enterprise_use
    v = Environment.new
    assert !v.has_terms_of_enterprise_use?
    v.terms_of_enterprise_use = 'some terms of enterprise use'
    assert v.has_terms_of_enterprise_use?
  end

  def test_should_list_top_level_categories
    env = Environment.create!(:name => 'a test environment')
    cat1 = Category.create!(:name => 'first category', :environment_id => env.id)
    cat2 = Category.create!(:name => 'second category', :environment_id => env.id)
    subcat = Category.create!(:name => 'child category', :environment_id => env.id, :parent_id => cat2.id)

    cats = env.top_level_categories
    assert_equal 2, cats.size
    assert cats.include?(cat1)
    assert cats.include?(cat2)
    assert !cats.include?(subcat)
  end

  def test_should_list_all_categories
    env = Environment.create!(:name => 'a test environment')
    cat1 = Category.create!(:name => 'first category', :environment_id => env.id)
    cat2 = Category.create!(:name => 'second category', :environment_id => env.id)
    subcat = Category.create!(:name => 'child category', :environment_id => env.id, :parent_id => cat2.id)

    cats = env.categories
    assert_equal 3, cats.size
    assert cats.include?(cat1)
    assert cats.include?(cat2)
    assert cats.include?(subcat)
  end

  should 'list displayable categories' do
    env = Environment.create!(:name => 'a test environment')
    cat1 = env.categories.create(:name => 'category one', :display_color => 1)
    assert ! cat1.new_record?

    # subcategories should be ignored
    subcat1 = env.categories.create(:name => 'subcategory one', :parent_id => cat1.id)
    assert ! subcat1.new_record?

    cat2 = env.categories.create(:name => 'category two')
    assert !cat2.new_record?

    assert_equal 1,  env.display_categories.size
    assert env.display_categories.include?(cat1)
    assert !env.display_categories.include?(cat2)
  end

  should 'have regions' do
    env = Environment.create!(:name => 'a test environment')
    assert_kind_of Array, env.regions
    assert_raise ActiveRecord::AssociationTypeMismatch do
      env.regions << 1
    end
    assert_nothing_raised do
      env.regions << Region.new
    end
  end

  should 'have a contact email' do
    env = Environment.new
    assert_nil env.contact_email

    env.contact_email = 'test'
    env.valid?
    assert env.errors.invalid?(:contact_email)

    env.contact_email = 'test@example.com'
    env.valid?
    assert !env.errors.invalid?(:contact_email)
  end

  should 'provide a default hostname' do
    env = Environment.create!(:name => 'test environment')
    env.domains << Domain.create(:name => 'example.com')
    assert_equal 'example.com', env.default_hostname
  end

  should 'default to localhost as hostname' do
    env = Environment.create!(:name => 'test environment')
    assert_equal 'localhost', env.default_hostname
  end

  should 'add www when told to force www' do
    env = Environment.create!(:name => 'test environment', :force_www => true)
    env.domains << Domain.create(:name => 'example.com')
    assert_equal 'www.example.com', env.default_hostname
  end

  should 'provide default top URL' do
    env = Environment.new
    env.expects(:default_hostname).returns('www.lalala.net')
    assert_equal 'http://www.lalala.net', env.top_url
  end

  should 'include port in default top URL for development environment' do
    env = Environment.new
    env.expects(:default_hostname).returns('www.lalala.net')

    Noosfero.expects(:url_options).returns({ :port => 9999 }).at_least_once

    assert_equal 'http://www.lalala.net:9999', env.top_url
  end

  should 'use https when asked for a ssl url' do
    env = Environment.new
    env.expects(:default_hostname).returns('www.lalala.net')
    assert_equal 'https://www.lalala.net', env.top_url(true)
  end

  should 'provide an approval_method setting' do
    env = Environment.new

    # default value
    assert_equal :admin, env.organization_approval_method

    # valid values
    assert_nothing_raised do
      valid = %w[
        admin
        region
      ].each do |item|
        env.organization_approval_method = item
        env.organization_approval_method = item.to_sym
      end
    end

    # do not allow other values
    assert_raise ArgumentError do
      env.organization_approval_method = :lalala
    end
    
  end

  should 'provide environment name in to_s' do
    env = Environment.new(:name => 'my name')
    assert_equal 'my name', env.to_s
  end

  should 'fallback to "?" when calling to_s with empty name' do
    env = Environment.new(:name => nil)
    assert_nil env.name
    assert_equal "?", env.to_s
  end

  should 'remove boxes and blocks when removing environment' do
    env = Environment.create!(:name => 'test environment')

    env_boxes = env.boxes.size
    env_blocks = env.blocks.size
    assert env_boxes > 0
    assert env_blocks > 0

    boxes = Box.count
    blocks = Block.count

    env.destroy

    assert_equal boxes - env_boxes, Box.count
    assert_equal blocks - env_blocks, Block.count
  end
  
  should 'have boxes and blocks upon creation' do
    environment = Environment.create!(:name => 'a test environment')
    assert environment.boxes.size > 0
    assert environment.blocks.size > 0
  end

  should 'have at least one MainBlock upon creation' do
    environment = Environment.create!(:name => 'a test environment')
    assert(environment.blocks.any? { |block| block.kind_of? MainBlock })
  end

  should 'provide recent_documents' do
    environment = Environment.create(:name => 'a test environment')

    p1 = environment.profiles.build(:identifier => 'testprofile1', :name => 'test profile 1'); p1.save!
    p2 = environment.profiles.build(:identifier => 'testprofile2', :name => 'test profile 2'); p2.save!

    # clear the articles
    Article.destroy_all

    # p1 creates one article
    doc1 = p1.articles.build(:name => 'text 1'); doc1.save!

    # p2 creates two articles
    doc2 = p2.articles.build(:name => 'text 2'); doc2.save!
    doc3 = p2.articles.build(:name => 'text 3'); doc3.save!

    # p1 creates another article
    doc4 = p1.articles.build(:name => 'text 4'); doc4.save!

    all_recent = environment.recent_documents
    [doc1,doc2,doc3,doc4].each do |item|
      assert_includes all_recent, item
    end

    last_three = environment.recent_documents(3)
    [doc2, doc3, doc4].each do |item|
      assert_includes last_three, item
    end
    assert_not_includes last_three, doc1

  end

  should 'have a description attribute' do
    env = Environment.new

    env.description = 'my fine environment'
    assert_equal 'my fine environment', env.description
  end

  should 'have admin role' do
    Role.expects(:find_by_key).with('environment_administrator').returns(Role.new)
    assert_kind_of Role, Environment::Roles.admin
  end

  should 'have products through enterprises' do
    env = Environment.default
    e1 = Enterprise.create!(:name => 'test_ent1', :identifier => 'test_ent1')
    p1 = e1.products.create!(:name => 'test_prod1')

    assert_includes env.products, p1
  end
  
  should 'not have person through communities' do
    env = Environment.default
    com = Community.create!(:identifier => 'community_1', :name => 'Community one')
    person = create_user('test_user').person
    assert_includes env.communities, com
    assert_not_includes env.communities, person
  end

  should 'not have person through enterprises' do
    env = Environment.default
    ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one')
    person = create_user('test_user').person
    assert_includes env.enterprises, ent
    assert_not_includes env.enterprises, person
  end

  should 'not have enterprises through people' do
    env = Environment.default
    person = create_user('test_user').person
    ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one')
    assert_includes env.people, person
    assert_not_includes env.people, ent
  end

  should 'have a message_for_disabled_enterprise attribute' do
    env = Environment.new
    env.message_for_disabled_enterprise = 'this enterprise was disabled'
    assert_equal 'this enterprise was disabled', env.message_for_disabled_enterprise
  end

  should 'have articles and text_articles' do
    # FIXME 
    assert true
    #environment = Environment.create(:name => 'a test environment')

    ## creates profile
    #profile = environment.profiles.create!(:identifier => 'testprofile1', :name => 'test profile 1')

    ## profile creates one article
    #article = profile.articles.create!(:name => 'text article')

    ## profile creates one textile article
    #textile = TextileArticle.create!(:name => 'textile article', :profile => profile)
    #profile.articles << textile

    #assert_includes environment.articles, article
    #assert_includes environment.articles, textile

    #assert_includes environment.text_articles, textile
    #assert_not_includes environment.text_articles, article
  end

  should 'find by contents from articles' do
    environment = Environment.create(:name => 'a test environment')
    assert_nothing_raised do
      environment.articles.find_by_contents('')
      # FIXME
      #environment.text_articles.find_by_contents('')
    end
  end

  should 'provide custom header' do
    assert_equal 'my header', Environment.new(:custom_header => 'my header').custom_header
  end

  should 'provide custom footer' do
    assert_equal 'my footer', Environment.new(:custom_footer => "my footer").custom_footer
  end

  should 'provide theme' do
    assert_equal 'my-custom-theme', Environment.new(:theme => 'my-custom-theme').theme
  end

  should 'give default theme' do
    assert_equal 'default', Environment.new.theme
  end

  should 'create templates' do
    e = Environment.create!(:name => 'test_env')
    e.reload

    assert_kind_of Enterprise, e.enterprise_template
    assert_kind_of Community, e.community_template
    assert_kind_of Person, e.person_template
  end

  should 'have private templates' do
    e = Environment.create!(:name => 'test_env')
    e.reload

    assert !e.enterprise_template.public?
    assert !e.community_template.public?
    assert !e.person_template.public?
  end

end