event_test.rb
6.08 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
require File.dirname(__FILE__) + '/../test_helper'
class EventTest < ActiveSupport::TestCase
  should 'be an article' do
    assert_kind_of Article, Event.new
  end
  should 'provide description' do
    assert_kind_of String, Event.description
  end
  should 'provide short description' do
    assert_kind_of String, Event.short_description
  end
  should 'have a description' do
    e = Event.new(:description => 'some useful description')
    assert_equal 'some useful description', e.description
  end
  should 'have a link' do
    e = Event.new(:link => 'http://some.nice.site/')
    assert_equal 'http://some.nice.site/', e.link
  end
  should 'have an address' do
    e = Event.new(:address => 'South Noosfero street, 88')
    assert_equal 'South Noosfero street, 88', e.address
  end
  should 'have a start date' do
    e = Event.new
    e.start_date = Date.today
    assert_kind_of Date, e.start_date
  end
  should 'require start date' do
    e = Event.new
    e.valid?
    assert e.errors.invalid?(:start_date)
    e.start_date = Date.today
    e.valid?
    assert !e.errors.invalid?(:start_date)
  end
  should 'have a end date' do
    e = Event.new
    e.end_date = Date.today
    assert_kind_of Date, e.end_date
  end
  should 'be indexed by title' do
    profile = create_user('testuser').person
    e = Event.create!(:name => 'my surprisingly nice event', :start_date => Date.new(2008, 06, 06), :profile => profile)
    assert_includes Event.find_by_contents('surprisingly'), e
  end
  should 'be indexed by description' do
    profile = create_user('testuser').person
    e = Event.create!(:name => 'bli', :start_date => Date.new(2008, 06, 06), :profile => profile, :description => 'my surprisingly long description about my freaking nice event')
    assert_includes Event.find_by_contents('surprisingly'), e
  end
  should 'use its own icon' do
    assert_equal 'event', Event.new.icon_name
  end
  should 'not allow end date before start date' do
    e = Event.new(:start_date => Date.new(2008, 01, 01), :end_date => Date.new(2007,01,01))
    e.valid?
    assert e.errors.invalid?(:start_date)
    e.end_date = Date.new(2008,01,05)
    e.valid?
    assert !e.errors.invalid?(:start_date)
  end
  should 'find by year and month' do
    profile = create_user('testuser').person
    e1 = Event.create!(:name => 'e1', :start_date =>  Date.new(2008,1,1), :profile => profile)
    e2 = Event.create!(:name => 'e2', :start_date =>  Date.new(2008,2,1), :profile => profile)
    e3 = Event.create!(:name => 'e3', :start_date =>  Date.new(2008,3,1), :profile => profile)
    found = Event.by_month(2008, 2)
    assert_includes found, e2
    assert_not_includes found, e1
    assert_not_includes found, e3
  end
  should 'find when in first day of month' do
    profile = create_user('testuser').person
    e1 = Event.create!(:name => 'e1', :start_date =>  Date.new(2008,1,1), :profile => profile)
    assert_includes Event.by_month(2008, 1), e1
  end
  should 'find when in last day of month' do
    profile = create_user('testuser').person
    e1 = Event.create!(:name => 'e1', :start_date =>  Date.new(2008,1,31), :profile => profile)
    assert_includes Event.by_month(2008, 1), e1
  end
  should 'use current month by default' do
    profile = create_user('testuser').person
    e1 = Event.create!(:name => 'e1', :start_date =>  Date.new(2008,1,31), :profile => profile)
    Date.expects(:today).returns(Date.new(2008, 1, 15))
    assert_includes Event.by_month, e1
  end
  should 'provide period for searching in month' do
    assert_equal Date.new(2008, 1, 1)..Date.new(2008,1,31), Event.date_range(2008, 1)
    assert_equal Date.new(2008, 2, 1)..Date.new(2008,2,29), Event.date_range(2008, 2)
    assert_equal Date.new(2007, 2, 1)..Date.new(2007,2,28), Event.date_range(2007, 2)
  end
  should 'support string arguments to Event#date_range' do
    assert_equal Date.new(2008,1,1)..Date.new(2008,1,31), Event.date_range('2008', '1')
  end
  should 'provide range of dates for event with both dates filled' do
    e = Event.new(:start_date => Date.new(2008, 1, 1), :end_date => Date.new(2008, 1, 5))
    
    assert_equal (Date.new(2008,1,1)..Date.new(2008,1,5)), e.date_range
  end
  should 'provide range of dates for event with only start date' do
    e = Event.new(:start_date => Date.new(2008, 1, 1))
    
    assert_equal (Date.new(2008,1,1)..Date.new(2008,1,1)), e.date_range
  end
  should 'provide nice display format' do
    e = Event.new(:start_date => Date.new(2008,1,1), :end_date => Date.new(2008,1,1), :link => 'http://www.myevent.org', :description => 'my somewhat short description')
    assert_tag_in_string e.to_html, :content => Regexp.new("January 1, 2008")
    assert_tag_in_string e.to_html, :content => 'my somewhat short description'
    assert_tag_in_string e.to_html, :tag => 'a', :attributes => { :href  => 'http://www.myevent.org' }, :content => 'http://www.myevent.org'
    
  end
  should 'not crash when description is blank' do
    e = Event.new
    assert_nil e.description
    assert_no_match(/_____XXXX_DESCRIPTION_GOES_HERE_XXXX_____/, e.to_html)
  end
  should 'add http:// to the link if not already present' do
    a = Event.new(:link => 'www.nohttp.net')
    assert_equal 'http://www.nohttp.net', a.link
  end
  should 'add http:// when reading link' do
    a = Event.new
    a.body[:link] = 'www.gnu.org'
    assert_equal 'http://www.gnu.org', a.link
  end
  should 'not escape HTML in description' do
    a = Event.new(:description => '<p>a paragraph of text</p>', :link => 'www.gnu.org')
    assert_match '<p>a paragraph of text</p>', a.to_html
  end
  should 'filter HTML in description' do
    profile = create_user('testuser').person
    e = Event.create!(:profile => profile, :name => 'test', :description => '<p>a paragraph (valid)</p><script type="text/javascript">/* this is invalid */</script>"', :link => 'www.colivre.coop.br', :start_date => Date.today)
    assert_tag_in_string e.description, :tag => 'p', :content => 'a paragraph (valid)'
    assert_no_tag_in_string e.description, :tag => 'script'
  end
  should 'nil to link' do
    e = Event.new
    assert_nothing_raised TypeError do
      e.link = nil
    end
  end
end