themes_controller_test.rb
10.7 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
require File.dirname(__FILE__) + '/../test_helper'
require 'themes_controller'
class ThemesController; def rescue_action(e) raise e end; end
class ThemesControllerTest < Test::Unit::TestCase
  def setup
    @controller = ThemesController.new
    @request    = ActionController::TestRequest.new
    @request.stubs(:ssl?).returns(true)
    @response   = ActionController::TestResponse.new
    Theme.stubs(:user_themes_dir).returns(TMP_THEMES_DIR)
    @profile = create_user('testinguser').person
    login_as('testinguser')
    @env = Environment.default
    @env.enable('user_themes')
    @env.save!
  end
  attr_reader :profile, :env
  def teardown
    FileUtils.rm_rf(TMP_THEMES_DIR)
  end
  TMP_THEMES_DIR = RAILS_ROOT + '/test/tmp/themes_controller'
  should 'display themes that can be applied' do
    env = Environment.default
    Theme.stubs(:approved_themes).with(@profile).returns([Theme.new('t1', :name => 't1')])
    t2 = Theme.create('t2')
    t3 = Theme.create('t3')
    env.themes = [t2]
    env.save
    Theme.stubs(:system_themes).returns([t2, t3])
    get :index, :profile => 'testinguser'
    %w[ t1 t2 ].each do |item|
      assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/#{item}" }
    end
    assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/t3" }
  end
  should 'highlight current theme' do
    env = Environment.default
    t1 = Theme.create('one', :name => 'one')
    t2 = Theme.create('two', :name => 'two')
    env.themes = [t1, t2]
    env.save
    Theme.stubs(:system_themes).returns([t1, t2])
    profile.update_attributes(:theme => 'one')
    get :index, :profile => 'testinguser'
    assert_tag :attributes => { :class => 'selected theme' }, :descendant =>  { :content => /(current)/ }
    assert_no_tag :attributes => { :class => 'selected theme' }, :descendant =>  { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/one" } }
  end
  should 'display list of my themes for edition' do
    Theme.create('three', :owner => profile)
    Theme.create('four', :owner => profile)
    get :index, :profile => 'testinguser'
    %w[ three four ].each do |item|
      assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/edit/#{item}" }
    end
  end
  should 'save selection of theme' do
    get :set, :profile => 'testinguser', :id => 'onetheme'
    profile = Profile.find(@profile.id)
    assert_equal 'onetheme', profile.theme
  end
  should 'point back to control panel' do
    get :index, :profile => 'testinguser'
    assert_tag :tag => 'a', :attributes => { :href =>  '/myprofile/testinguser' }, :content => 'Back'
  end
  should 'display screen for creating new theme' do
    @request.expects(:xhr?).returns(true).at_least_once
    get :new, :profile => 'testinguser'
    assert_tag :tag => 'form', :attributes => { :action => '/myprofile/testinguser/themes/new', :method => /post/i }, :descendant => { :tag => 'input', :attributes => { :type => 'text', :name => 'name' } }
  end
  should 'create a new theme' do
    post :new, :profile => 'testinguser', :name => 'My theme'
    
    ok('theme should be created') do
      profile.themes.first.id == 'my-theme'
    end
  end
  should 'edit a theme' do
    theme = Theme.create('mytheme', :owner => profile)
    get :edit, :profile => 'testinguser', :id => 'mytheme'
    assert_equal theme, assigns(:theme)
  end
  should 'list CSS files in theme' do
    theme = Theme.create('mytheme', :owner => profile)
    theme.add_css('one.css')
    theme.add_css('two.css')
    get :edit, :profile => 'testinguser', :id => 'mytheme'
    %w[ one.css two.css ].each do |item|
      assert_includes assigns(:css_files), item
      assert_tag :tag => 'li', :descendant => { :tag => 'a', :content => item}
    end
  end
  should 'display dialog for creating new CSS' do
    theme = Theme.create('mytheme', :owner => profile)
    @request.expects(:xhr?).returns(true)
    get :add_css, :profile => 'testinguser', :id => 'mytheme'
    assert_tag :tag => 'form', :attributes => { :action => '/myprofile/testinguser/themes/add_css/mytheme', :method => /post/i}
    assert_tag :tag => 'input', :attributes => { :name => 'css', :type => 'text' }
    assert_tag :tag => 'input', :attributes => { :type => 'submit' }
  end
  should 'be able to add new CSS to theme' do
    theme = Theme.create('mytheme', :owner => profile)
    post :add_css, :profile => 'testinguser', :id => 'mytheme', :css => 'test.css'
    assert_response :redirect
    reloaded_theme = Theme.find('mytheme')
    assert_includes reloaded_theme.css_files, 'test.css'
  end
  should 'load code from a given CSS file' do
    theme = Theme.create('mytheme', :owner => profile); theme.update_css('test.css', '/* sample code */')
    get :css_editor, :profile => 'testinguser', :id => 'mytheme', :css => 'test.css'
    assert_tag :tag => 'form', :attributes => { :action => '/myprofile/testinguser/themes/update_css/mytheme' }, :descendant => { :tag => 'textarea', :content => '/* sample code */' }
  end
  should 'be able to save CSS code' do
    theme = Theme.create('mytheme', :owner => profile); theme.update_css('test.css', '/* sample code */')
    get :css_editor, :profile => 'testinguser', :id => 'mytheme', :css => 'test.css'
    assert_tag :tag => 'form', :attributes => { :action => '/myprofile/testinguser/themes/update_css/mytheme' }, :descendant => { :tag => 'input', :attributes => { :type => 'submit' } }
    assert_tag :tag => 'form', :attributes => { :action => '/myprofile/testinguser/themes/update_css/mytheme' }, :descendant => { :tag => 'input', :attributes => { :type => 'hidden', :name => 'css', :value => 'test.css' } }
  end
  should 'update css code when saving' do
    theme = Theme.create('mytheme', :owner => profile); theme.update_css('test.css', '/* sample code */')
    post :update_css, :profile => 'testinguser', :id => 'mytheme', :css => 'test.css', :csscode => 'body { background: white; }'
    assert_equal 'body { background: white; }', theme.read_css('test.css')
  end
  should 'list image files in theme' do
    theme = Theme.create('mytheme', :owner => profile)
    theme.add_image('one.png', 'FAKE IMAGE DATA 1')
    theme.add_image('two.png', 'FAKE IMAGE DATA 2')
    get :edit, :profile => 'testinguser', :id => 'mytheme'
    assert_tag :tag => 'img', :attributes => { :src => '/user_themes/mytheme/images/one.png' }
    assert_tag :tag => 'img', :attributes => { :src => '/user_themes/mytheme/images/two.png' }
  end
  should 'display "add image" button' do
    theme = Theme.create('mytheme', :owner => profile)
    get :edit, :profile => 'testinguser', :id => 'mytheme'
    
    assert_tag :tag => 'a', :attributes => { :href => '/myprofile/testinguser/themes/add_image/mytheme' }
  end
  should 'display the "add image" dialog' do
    theme = Theme.create('mytheme', :owner => profile)
    @request.expects(:xhr?).returns(true)
    get :add_image, :profile => 'testinguser', :id => 'mytheme'
    assert_tag :tag => 'form', :attributes => { :action => '/myprofile/testinguser/themes/add_image/mytheme', :method => /post/i, :enctype => 'multipart/form-data' }, :descendant => { :tag => 'input', :attributes => { :name => 'image', :type => 'file' } }
  end
  should 'be able to add new image to theme' do
    theme = Theme.create('mytheme', :owner => profile)
    @request.expects(:xhr?).returns(false)
    post :add_image, :profile => 'testinguser', :id => 'mytheme', :image => fixture_file_upload('/files/rails.png', 'image/png', :binary)
    assert_redirected_to :action => "edit", :id => 'mytheme'
    assert theme.image_files.include?('rails.png')
    assert(system('diff', RAILS_ROOT + '/test/fixtures/files/rails.png', TMP_THEMES_DIR + '/mytheme/images/rails.png'), 'should put the correct uploaded file in the right place')
  end
  should 'link to "test theme"' do
    Theme.create('one', :owner => profile)
    Theme.create('two', :owner => profile)
    get :index, :profile => 'testinguser'
    %w[ one two ].each do |item|
      assert_tag :tag => 'a', :attributes => { :href => '/myprofile/testinguser/themes/start_test/' + item }
    end
  end
  should 'start testing theme' do
    theme = Theme.create('theme-under-test', :owner => profile)
    post :start_test, :profile => 'testinguser', :id => 'theme-under-test'
    assert_equal 'theme-under-test', session[:theme]
    assert_redirected_to :controller => 'content_viewer', :profile => 'testinguser'
  end
  should 'stop testing theme' do
    theme = Theme.create('theme-under-test', :owner => profile)
    post :stop_test, :profile => 'testinguser', :id => 'theme-under-test'
    assert_nil session[:theme]
    assert_redirected_to :action => 'index'
  end
  should 'list templates' do
    all = LayoutTemplate.all
    LayoutTemplate.expects(:all).returns(all)
    get :index, :profile => 'testinguser'
    assert_same all, assigns(:layout_templates)
  end
  should 'display links to set template' do
    profile.update_attributes!(:layout_template => 'rightbar')
    t1 = LayoutTemplate.find('default')
    t2 = LayoutTemplate.find('leftbar')
    LayoutTemplate.expects(:all).returns([t1, t2])
    get :index, :profile => 'testinguser'
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set_layout_template/default"}
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set_layout_template/leftbar"}
  end
  should 'highlight current template' do
    profile.update_attributes!(:layout_template => 'default')
    t1 = LayoutTemplate.find('default')
    t2 = LayoutTemplate.find('leftbar')
    LayoutTemplate.expects(:all).returns([t1, t2])
    get :index, :profile => 'testinguser'
    assert_tag :tag => 'td', :attributes => { :class => 'selected template' }, :descendant => { :content => /(current)/ }
    assert_no_tag :tag => 'td', :attributes => { :class => 'selected template' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set_layout_template/default"} }
  end
  should 'set template' do
    post :set_layout_template, :profile => 'testinguser', :id => 'leftbar'
    profile = Profile.find(@profile.id)
    assert_equal 'leftbar', profile.layout_template
    assert_redirected_to :action => 'index'
  end
  should 'not display "new theme" button when user themes are disabled' do
    env.disable('user_themes')
    env.save!
    get :index, :profile => 'testinguser'
    assert_no_tag :tag => 'a', :attributes => { :href => '/myprofile/testinguser/themes/new' }
  end
  should 'not display the "Select themes" section if there are no themes to choose from' do
    env.themes = []; env.save!
    Theme.stubs(:system_themes_dir).returns(TMP_THEMES_DIR) # an empty dir
    get :index, :profile => "testinguser"
    assert_no_tag :content => "Select theme"
  end
end