cirandas.net

ref: master

test/functional/categories_controller_test.rb


  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
require_relative '../test_helper'

class CategoriesControllerTest < ActionController::TestCase
  all_fixtures
  def setup
    @controller = CategoriesController.new

    @env = fast_create(Environment, :name => "My test environment")
    Environment.stubs(:default).returns(env)
    assert (@cat1 = env.categories.create(:name => 'a test category'))
    assert (@cat1 = env.categories.create(:name => 'another category'))
    login_as(create_admin_user(@env))
  end

  attr_reader :env, :cat1, :cat2

  def test_index
    login_as(create_admin_user(Environment.default))
    get :index
    assert_response :success
    assert_template 'index'
    assert_tag :tag => 'a', :attributes => { :href => '/admin/categories/new'}
  end

  def test_edit
    get :edit, :id => cat1
    assert_response :success
    assert_template 'edit'
    assert_equal cat1, assigns(:category)
  end

  def test_edit_save
    post :edit, :id => cat1.id, :category => { :name => 'new name for category', :display_color => nil }
    assert_redirected_to :action => 'index'
    assert_equal 'new name for category', Category.find(cat1.id).name
  end

  def test_new_category
    cat = Category.new
    Category.expects(:new).returns(cat)
    get :new
  end

  def test_new_save
    assert_difference 'Category.count' do
      post :new, :category => { :name => 'a new category' }
      assert_redirected_to :action => 'index'
    end
  end

  def test_remove
    cat = create(Category, :name => 'a category to be removed', :environment_id => env.id)
    post :remove, :id => cat.id
    assert_redirected_to :action => 'index'
    assert_raise ActiveRecord::RecordNotFound do
      Category.find(cat.id)
    end
  end

  should 'be able to upload a file' do
    assert_difference 'Category.count' do
      post :new, :category => { :name => 'new category', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
      assert_equal assigns(:category).image.filename, 'rails.png'
    end
  end

  should 'expire categories menu cache when some menu category is updated' do
    cat = create(Category, :name => 'test category in menu', :environment => Environment.default, :display_in_menu => true)
    @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').at_least_once
    post :edit, :id => cat.id, :category => { :name => 'new name for category in menu' }
  end

  should 'not touch categories menu cache whem updated category is not in menu' do
    cat = create(Category, :name => 'test category not in menu', :environment => Environment.default, :display_in_menu => false)
    @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never
    post :edit, :id => cat.id, :category => { :name => 'new name for category not in menu' }
  end

  should 'expire categories menu cache when new category is created for the menu' do
    @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').at_least_once
    post :new, :category => { :name => 'my new category for the menu', :display_in_menu => '1' }
  end

  should 'not handle cache when viewing "edit category" screen' do
    cat = create(Category, :name => 'test category in menu', :environment => Environment.default, :display_in_menu => true)
    @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never
    get :edit, :id => cat.id
  end

  should 'not expire categories menu cache when new category is created, but not for the menu' do
    @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never
    post :new, :category => { :name => 'my new category for the menu', :display_in_menu => '0' }
  end

  should 'not handle cache when viewing "new category" screen' do
    @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never
    get :new
  end

  should 'not expire cache when updating fails' do
    cat = create(Category, :name => 'test category in menu', :environment => Environment.default, :display_in_menu => true)
    @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never

    post :edit, :id => cat.id, :category => { :name => '' }

    cat.reload
    assert_equal 'test category in menu', cat.name
  end

  should 'not expire cache when creating fails' do
    @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never
    post :new, :category => { :display_in_menu => '1' }
  end

  should 'not display color selection if environment.categories_menu is false' do
    env.enable('disable_categories_menu')
    env.save!
    get :new

    assert_no_tag :tag => 'input', :attributes => { :name => "category[display_color]" }
  end

  should 'display color selection if environment.categories_menu is true' do
    env.disable('disable_categories_menu')
    env.save!
    get :new

    assert_tag :tag => 'input', :attributes => { :name => "category[display_color]" }
  end

  should 'not list regions' do
    Environment.default.categories.destroy_all
    c = create(Category, :name => 'Regular category', :environment => Environment.default)
    r = create(Region, :name => 'Some region', :environment => Environment.default)

    get :index
    assert_equal [c], assigns(:categories)
    assert_equal [r], assigns(:regions)
  end

  should 'use parent\'s type to determine subcategory\'s type' do
    parent = create(Region, name: 'Sample category', environment: Environment.default)
    post :new, :parent_id => parent.id, :parent_type => parent.class.name, :category => {:name => 'Subcategory'}
    sub = Region.find_by_name('Subcategory')
    assert_equal parent.class, sub.class
  end

end