cirandas.net

ref: master

test/functional/profile_themes_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
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
require_relative "../test_helper"

class ProfileThemesControllerTest < ActionController::TestCase

  TMP_THEMES_DIR = 'test/tmp/profile_themes_controller'
  TMP_THEMES_PATH = File.join Rails.root, TMP_THEMES_DIR

  def setup
    @controller = ProfileThemesController.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.enable_default_features
    @env.save!
  end
  attr_reader :profile, :env

  def teardown
    FileUtils.rm_rf(TMP_THEMES_PATH)
  end

  TMP_THEMES_DIR = Rails.root.join("test", "tmp", "profile_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 = 't2'
    t3 = 't3'
    env.themes = [t2]
    env.save

    Theme.stubs(:system_themes).returns([Theme.new(t2), Theme.new(t3)])
    get :index, :profile => 'testinguser'

    %w[ t1 t2 ].each do |item|
      assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/profile_themes/set/#{item}" }
    end

    assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/profile_themes/set/t3" }
  end

  should 'highlight current theme' do
    env = Environment.default
    t1 = 'one'
    t2 = 'two'
    env.themes = [t1, t2]
    env.save

    Theme.stubs(:system_themes).returns([Theme.new(t1), Theme.new(t2)])
    profile.update_theme(t1)
    get :index, :profile => 'testinguser'

    assert_tag :attributes => { :class => 'theme-opt list-opt selected' }
  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/profile_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 'save selection of theme even if model is invalid' do
    @profile.sex = nil
    @profile.save!
    @profile.environment.custom_person_fields = { 'sex' => {'required' => 'true', 'active' => 'true'} }; @profile.environment.save!

    get :set, :profile => 'testinguser', :id => 'onetheme'
    profile = Profile.find(@profile.id)
    assert_equal 'onetheme', profile.theme
  end

  should 'unset selection of theme' do
    get :unset, :profile => 'testinguser'
    assert_equal nil, profile.theme
  end

  should 'display link to use the default theme' do
    env = Environment.default
    env.themes = ['new-theme']
    env.save

    Theme.stubs(:system_themes).returns([Theme.new('new-theme')])

    get :index, :profile => 'testinguser'
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/profile_themes/unset" }
  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/profile_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.stubs(:xhr?).returns(true)
    get :add_css, :profile => 'testinguser', :id => 'mytheme'

    assert_tag :tag => 'form', :attributes => { :action => '/myprofile/testinguser/profile_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/profile_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/profile_themes/update_css/mytheme' }, :descendant => { :tag => 'input', :attributes => { :type => 'submit' } }
    assert_tag :tag => 'form', :attributes => { :action => '/myprofile/testinguser/profile_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 => "/#{TMP_THEMES_DIR}/mytheme/images/one.png" }
    assert_tag :tag => 'img', :attributes => { :src => "/#{TMP_THEMES_DIR}/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/profile_themes/add_image/mytheme' }
  end

  should 'display the "add image" dialog' do
    theme = Theme.create('mytheme', :owner => profile)
    @request.stubs(:xhr?).returns(true)

    get :add_image, :profile => 'testinguser', :id => 'mytheme'
    assert_tag :tag => 'form', :attributes => { :action => '/myprofile/testinguser/profile_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.stubs(: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.join('test', 'fixtures', 'files','rails.png').to_s, TMP_THEMES_PATH.join('mytheme/images/rails.png').to_s), '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/profile_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[:user_theme]
    assert_redirected_to :controller => 'content_viewer', :profile => 'testinguser', :action => 'view_page'
  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[:user_theme]
    assert_redirected_to :action => 'index'
  end

  should 'list templates' do
    all = LayoutTemplate.all

    LayoutTemplate.expects(:all).returns(all)
    get :index, :profile => 'testinguser'
    assert_equal all, assigns(:layout_templates)
  end

  should 'display links to set template' do
    profile.layout_template = 'rightbar'
    profile.save!
    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/profile_themes/set_layout_template/default"}
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/profile_themes/set_layout_template/leftbar"}
  end

  should 'highlight current template' do
    profile.layout_template = 'default'
    profile.save!

    t1 = LayoutTemplate.find('default')
    t2 = LayoutTemplate.find('leftbar')
    LayoutTemplate.expects(:all).returns([t1, t2])

    get :index, :profile => 'testinguser'
    assert_tag :attributes => { :class => 'template-opt list-opt selected' }
    assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/profile_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 'set template even if the model is invalid' do
    @profile.sex = nil
    @profile.save!
    @profile.environment.custom_person_fields = { 'sex' => {'required' => 'true', 'active' => 'true'} }; @profile.environment.save!

    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/profile_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

  should 'not duplicate themes that are included by the user and by the environment' do
    t1 = Theme.create('theme1')
    t2 = Theme.create('theme2')
    Environment.any_instance.stubs('themes').returns([t1,t2])
    Theme.stubs(:approved_themes).returns([t2])

    get :index, :profile => "testinguser"
    assert_equivalent [t1, t2], assigns(:themes)
  end

  should 'sort themes by name' do
    t1 = Theme.create('bill-theme')
    t2 = Theme.create('ana-theme')
    Theme.stubs(:approved_themes).returns([t1,t2])

    get :index, :profile => "testinguser"
    assert_equal [t2, t1], assigns(:themes)
  end

  should 'user cant edit appearance if environment dont permit' do
    environment = Environment.default
    environment.disable('enable_appearance')
    environment.save!

    user = create_user('user').person
    login_as('user')

    post :index, :profile => user.identifier
    assert_response :redirect
  end

  should 'admin can edit appearance if environment dont permit' do
    user = create_user('user').person

    environment = Environment.default
    environment.add_admin(user)
    environment.disable('enable_appearance')
    environment.save!

    login_as('user')

    post :index, :profile => user.identifier
    assert_response :success
  end
end