highlights_block_test.rb
5.07 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
require File.dirname(__FILE__) + '/../test_helper'
class HighlightsBlockTest < ActiveSupport::TestCase
  should 'default describe' do
    assert_not_equal Block.description, HighlightsBlock.description
  end
  should 'have field images' do
    h = HighlightsBlock.new
    assert_respond_to h, :images
  end
  should 'have field interval' do
    h = HighlightsBlock.new
    assert_respond_to h, :interval
  end
  should 'have field shuffle' do
    h = HighlightsBlock.new
    assert_respond_to h, :shuffle
  end
  should 'have field navigation' do
    h = HighlightsBlock.new
    assert_respond_to h, :navigation
  end
  should 'default value of images' do
    h = HighlightsBlock.new
    assert_equal [], h.images
  end
  should 'default interval between transitions is 4 seconds' do
    h = HighlightsBlock.new
    assert_equal 4, h.interval
  end
  should 'default value of shuffle' do
    h = HighlightsBlock.new
    assert_equal false, h.shuffle
  end
  should 'default value of navigation' do
    h = HighlightsBlock.new
    assert_equal false, h.navigation
  end
  should 'is editable' do
    h = HighlightsBlock.new
    assert h.editable?
  end
  should 'remove images with blank fields' do
    h = HighlightsBlock.new(:images => [{:image_id => 1, :address => '/address', :position => 1, :title => 'address'}, {:image_id => '', :address => '', :position => '', :title => ''}])
    h.save!
    assert_equal [{:image_id => 1, :address => '/address', :position => 1, :title => 'address', :image_src => nil}], h.images
  end
  should 'be able to update display setting' do
    user = create_user('testinguser').person
    box = fast_create(Box, :owner_id => user.id)
    block = HighlightsBlock.create!(:display => 'never', :box => box)
    assert block.update_attributes!(:display => 'always')
    block.reload
    assert_equal 'always', block.display
  end
  should 'display highlights block' do
    block = HighlightsBlock.new
    self.expects(:render).with(:file => 'blocks/highlights', :locals => { :block => block})
    instance_eval(& block.content)
  end
  should 'not list non existent image' do
    file = mock()
    UploadedFile.expects(:find).with(1).returns(file)
    file.expects(:public_filename).returns('address')
    UploadedFile.expects(:find).with(0).returns(nil)
    block = HighlightsBlock.new(:images => [{:image_id => 1, :address => '/address', :position => 1, :title => 'address'}, {:image_id => '', :address => 'some', :position => '2', :title => 'Some'}])
    block.save!
    block.reload
    assert_equal 2, block.images.count
    assert_equal [{:image_id => 1, :address => '/address', :position => 1, :title => 'address', :image_src => 'address'}], block.featured_images
  end
  should 'list images in order' do
    f1 = mock()
    f1.expects(:public_filename).returns('address')
    UploadedFile.expects(:find).with(1).returns(f1)
    f2 = mock()
    f2.expects(:public_filename).returns('address')
    UploadedFile.expects(:find).with(2).returns(f2)
    f3 = mock()
    f3.expects(:public_filename).returns('address')
    UploadedFile.expects(:find).with(3).returns(f3)
    block = HighlightsBlock.new
    i1 = {:image_id => 1, :address => '/address', :position => 3, :title => 'address'}
    i2 = {:image_id => 2, :address => '/address', :position => 1, :title => 'address'}
    i3 = {:image_id => 3, :address => '/address', :position => 2, :title => 'address'}
    block.images = [i1,i2,i3]
    block.save!
    block.reload
    assert_equal [i1,i2,i3], block.images
    assert_equal [i2,i3,i1], block.featured_images
  end
  should 'list images randomically' do
    f1 = mock()
    f1.expects(:public_filename).returns('address')
    UploadedFile.expects(:find).with(1).returns(f1)
    f2 = mock()
    f2.expects(:public_filename).returns('address')
    UploadedFile.expects(:find).with(2).returns(f2)
    f3 = mock()
    f3.expects(:public_filename).returns('address')
    UploadedFile.expects(:find).with(3).returns(f3)
    f4 = mock()
    f4.expects(:public_filename).returns('address')
    UploadedFile.expects(:find).with(4).returns(f4)
    f5 = mock()
    f5.expects(:public_filename).returns('address')
    UploadedFile.expects(:find).with(5).returns(f5)
    block = HighlightsBlock.new
    i1 = {:image_id => 1, :address => '/address', :position => 3, :title => 'address'}
    i2 = {:image_id => 2, :address => '/address', :position => 1, :title => 'address'}
    i3 = {:image_id => 3, :address => '/address', :position => 2, :title => 'address'}
    i4 = {:image_id => 4, :address => '/address', :position => 5, :title => 'address'}
    i5 = {:image_id => 5, :address => '/address', :position => 4, :title => 'address'}
    block.images = [i1,i2,i3,i4,i5]
    block.shuffle = true
    block.save!
    block.reload
    assert_equal [i1,i2,i3,i4,i5], block.images
    assert_not_equal [i2,i3,i1,i4,i5], block.featured_images
  end
  [Environment, Profile].each do |klass|
    should "choose between owner galleries when owner is #{klass.name}" do
      owner = fast_create(klass)
      block = HighlightsBlock.new
      block.stubs(:owner).returns(owner)
      assert_kind_of Array, block.folder_choices
    end
  end
end