Commit 045fbc74fe3f6e214d89a671f2bbead3a30bac05

Authored by Rodrigo Souto
1 parent 631b96a0

rails3: fix event tests

PS: still failing xss_terminate related tests
Showing 2 changed files with 28 additions and 25 deletions   Show diff stats
app/models/event.rb
1 require 'noosfero/translatable_content' 1 require 'noosfero/translatable_content'
  2 +require 'builder'
2 3
3 class Event < Article 4 class Event < Article
4 5
  6 + attr_accessible :start_date, :end_date, :link
  7 +
5 def self.type_name 8 def self.type_name
6 _('Event') 9 _('Event')
7 end 10 end
@@ -88,7 +91,7 @@ class Event &lt; Article @@ -88,7 +91,7 @@ class Event &lt; Article
88 def to_html(options = {}) 91 def to_html(options = {})
89 92
90 result = '' 93 result = ''
91 - html = Builder::XmlMarkup.new(:target => result) 94 + html = ::Builder::XmlMarkup.new(:target => result)
92 95
93 html.div(:class => 'event-info' ) { 96 html.div(:class => 'event-info' ) {
94 97
test/unit/event_test.rb
@@ -15,17 +15,17 @@ class EventTest &lt; ActiveSupport::TestCase @@ -15,17 +15,17 @@ class EventTest &lt; ActiveSupport::TestCase
15 end 15 end
16 16
17 should 'have a body' do 17 should 'have a body' do
18 - e = Event.new(:body => 'some useful description') 18 + e = build(Event, :body => 'some useful description')
19 assert_equal 'some useful description', e.body 19 assert_equal 'some useful description', e.body
20 end 20 end
21 21
22 should 'have a link' do 22 should 'have a link' do
23 - e = Event.new(:link => 'http://some.nice.site/') 23 + e = build(Event, :link => 'http://some.nice.site/')
24 assert_equal 'http://some.nice.site/', e.link 24 assert_equal 'http://some.nice.site/', e.link
25 end 25 end
26 26
27 should 'have an address' do 27 should 'have an address' do
28 - e = Event.new(:address => 'South Noosfero street, 88') 28 + e = build(Event, :address => 'South Noosfero street, 88')
29 assert_equal 'South Noosfero street, 88', e.address 29 assert_equal 'South Noosfero street, 88', e.address
30 end 30 end
31 31
@@ -61,7 +61,7 @@ class EventTest &lt; ActiveSupport::TestCase @@ -61,7 +61,7 @@ class EventTest &lt; ActiveSupport::TestCase
61 end 61 end
62 62
63 should 'not allow end date before start date' do 63 should 'not allow end date before start date' do
64 - e = Event.new(:start_date => Date.new(2008, 01, 01), :end_date => Date.new(2007,01,01)) 64 + e = build(Event, :start_date => Date.new(2008, 01, 01), :end_date => Date.new(2007,01,01))
65 e.valid? 65 e.valid?
66 assert e.errors[:start_date.to_s].present? 66 assert e.errors[:start_date.to_s].present?
67 67
@@ -72,9 +72,9 @@ class EventTest &lt; ActiveSupport::TestCase @@ -72,9 +72,9 @@ class EventTest &lt; ActiveSupport::TestCase
72 72
73 should 'find by range of dates' do 73 should 'find by range of dates' do
74 profile = create_user('testuser').person 74 profile = create_user('testuser').person
75 - e1 = Event.create!(:name => 'e1', :start_date => Date.new(2008,1,1), :profile => profile)  
76 - e2 = Event.create!(:name => 'e2', :start_date => Date.new(2008,2,1), :profile => profile)  
77 - e3 = Event.create!(:name => 'e3', :start_date => Date.new(2008,3,1), :profile => profile) 75 + e1 = create(Event, :name => 'e1', :start_date => Date.new(2008,1,1), :profile => profile)
  76 + e2 = create(Event, :name => 'e2', :start_date => Date.new(2008,2,1), :profile => profile)
  77 + e3 = create(Event, :name => 'e3', :start_date => Date.new(2008,3,1), :profile => profile)
78 78
79 found = Event.by_range(Date.new(2008, 1, 1)..Date.new(2008, 2, 28)) 79 found = Event.by_range(Date.new(2008, 1, 1)..Date.new(2008, 2, 28))
80 assert_includes found, e1 80 assert_includes found, e1
@@ -84,7 +84,7 @@ class EventTest &lt; ActiveSupport::TestCase @@ -84,7 +84,7 @@ class EventTest &lt; ActiveSupport::TestCase
84 84
85 should 'filter events by range' do 85 should 'filter events by range' do
86 profile = create_user('testuser').person 86 profile = create_user('testuser').person
87 - e1 = Event.create!(:name => 'e1', :start_date => Date.new(2008,1,15), :profile => profile) 87 + e1 = create(Event, :name => 'e1', :start_date => Date.new(2008,1,15), :profile => profile)
88 assert_includes profile.events.by_range(Date.new(2008, 1, 10)..Date.new(2008, 1, 20)), e1 88 assert_includes profile.events.by_range(Date.new(2008, 1, 10)..Date.new(2008, 1, 20)), e1
89 end 89 end
90 90
@@ -99,19 +99,19 @@ class EventTest &lt; ActiveSupport::TestCase @@ -99,19 +99,19 @@ class EventTest &lt; ActiveSupport::TestCase
99 end 99 end
100 100
101 should 'provide range of dates for event with both dates filled' do 101 should 'provide range of dates for event with both dates filled' do
102 - e = Event.new(:start_date => Date.new(2008, 1, 1), :end_date => Date.new(2008, 1, 5)) 102 + e = build(Event, :start_date => Date.new(2008, 1, 1), :end_date => Date.new(2008, 1, 5))
103 103
104 assert_equal (Date.new(2008,1,1)..Date.new(2008,1,5)), e.date_range 104 assert_equal (Date.new(2008,1,1)..Date.new(2008,1,5)), e.date_range
105 end 105 end
106 106
107 should 'provide range of dates for event with only start date' do 107 should 'provide range of dates for event with only start date' do
108 - e = Event.new(:start_date => Date.new(2008, 1, 1)) 108 + e = build(Event, :start_date => Date.new(2008, 1, 1))
109 109
110 assert_equal (Date.new(2008,1,1)..Date.new(2008,1,1)), e.date_range 110 assert_equal (Date.new(2008,1,1)..Date.new(2008,1,1)), e.date_range
111 end 111 end
112 112
113 should 'provide nice display format' do 113 should 'provide nice display format' do
114 - e = Event.new(:start_date => Date.new(2008,1,1), :end_date => Date.new(2008,1,1), :link => 'http://www.myevent.org', :body => 'my somewhat short description') 114 + e = build(Event, :start_date => Date.new(2008,1,1), :end_date => Date.new(2008,1,1), :link => 'http://www.myevent.org', :body => 'my somewhat short description')
115 115
116 assert_tag_in_string e.to_html, :content => Regexp.new("January 1, 2008") 116 assert_tag_in_string e.to_html, :content => Regexp.new("January 1, 2008")
117 assert_tag_in_string e.to_html, :content => 'my somewhat short description' 117 assert_tag_in_string e.to_html, :content => 'my somewhat short description'
@@ -126,7 +126,7 @@ class EventTest &lt; ActiveSupport::TestCase @@ -126,7 +126,7 @@ class EventTest &lt; ActiveSupport::TestCase
126 end 126 end
127 127
128 should 'add http:// to the link if not already present' do 128 should 'add http:// to the link if not already present' do
129 - a = Event.new(:link => 'www.nohttp.net') 129 + a = build(Event, :link => 'www.nohttp.net')
130 assert_equal 'http://www.nohttp.net', a.link 130 assert_equal 'http://www.nohttp.net', a.link
131 end 131 end
132 132
@@ -145,14 +145,14 @@ class EventTest &lt; ActiveSupport::TestCase @@ -145,14 +145,14 @@ class EventTest &lt; ActiveSupport::TestCase
145 end 145 end
146 146
147 should 'not escape HTML in body' do 147 should 'not escape HTML in body' do
148 - a = Event.new(:body => '<p>a paragraph of text</p>', :link => 'www.gnu.org') 148 + a = build(Event, :body => '<p>a paragraph of text</p>', :link => 'www.gnu.org')
149 149
150 assert_match '<p>a paragraph of text</p>', a.to_html 150 assert_match '<p>a paragraph of text</p>', a.to_html
151 end 151 end
152 152
153 should 'filter HTML in body' do 153 should 'filter HTML in body' do
154 profile = create_user('testuser').person 154 profile = create_user('testuser').person
155 - e = Event.create!(:profile => profile, :name => 'test', :body => '<p>a paragraph (valid)</p><script type="text/javascript">/* this is invalid */</script>"', :link => 'www.colivre.coop.br', :start_date => Date.today) 155 + e = create(Event, :profile => profile, :name => 'test', :body => '<p>a paragraph (valid)</p><script type="text/javascript">/* this is invalid */</script>"', :link => 'www.colivre.coop.br', :start_date => Date.today)
156 156
157 assert_tag_in_string e.body, :tag => 'p', :content => 'a paragraph (valid)' 157 assert_tag_in_string e.body, :tag => 'p', :content => 'a paragraph (valid)'
158 assert_no_tag_in_string e.body, :tag => 'script' 158 assert_no_tag_in_string e.body, :tag => 'script'
@@ -167,8 +167,8 @@ class EventTest &lt; ActiveSupport::TestCase @@ -167,8 +167,8 @@ class EventTest &lt; ActiveSupport::TestCase
167 167
168 should 'list all events' do 168 should 'list all events' do
169 profile = fast_create(Profile) 169 profile = fast_create(Profile)
170 - event1 = Event.new(:name => 'Ze Birthday', :start_date => Date.today)  
171 - event2 = Event.new(:name => 'Mane Birthday', :start_date => Date.today >> 1) 170 + event1 = build(Event, :name => 'Ze Birthday', :start_date => Date.today)
  171 + event2 = build(Event, :name => 'Mane Birthday', :start_date => Date.today >> 1)
172 profile.events << [event1, event2] 172 profile.events << [event1, event2]
173 assert_includes profile.events, event1 173 assert_includes profile.events, event1
174 assert_includes profile.events, event2 174 assert_includes profile.events, event2
@@ -178,9 +178,9 @@ class EventTest &lt; ActiveSupport::TestCase @@ -178,9 +178,9 @@ class EventTest &lt; ActiveSupport::TestCase
178 profile = fast_create(Profile) 178 profile = fast_create(Profile)
179 179
180 today = Date.today 180 today = Date.today
181 - yesterday_event = Event.new(:name => 'Joao Birthday', :start_date => today - 1.day)  
182 - today_event = Event.new(:name => 'Ze Birthday', :start_date => today)  
183 - tomorrow_event = Event.new(:name => 'Mane Birthday', :start_date => today + 1.day) 181 + yesterday_event = build(Event, :name => 'Joao Birthday', :start_date => today - 1.day)
  182 + today_event = build(Event, :name => 'Ze Birthday', :start_date => today)
  183 + tomorrow_event = build(Event, :name => 'Mane Birthday', :start_date => today + 1.day)
184 184
185 profile.events << [yesterday_event, today_event, tomorrow_event] 185 profile.events << [yesterday_event, today_event, tomorrow_event]
186 186
@@ -191,8 +191,8 @@ class EventTest &lt; ActiveSupport::TestCase @@ -191,8 +191,8 @@ class EventTest &lt; ActiveSupport::TestCase
191 profile = fast_create(Profile) 191 profile = fast_create(Profile)
192 192
193 today = Date.today 193 today = Date.today
194 - event_in_range = Event.new(:name => 'Noosfero Conference', :start_date => today - 2.day, :end_date => today + 2.day)  
195 - event_in_day = Event.new(:name => 'Ze Birthday', :start_date => today) 194 + event_in_range = build(Event, :name => 'Noosfero Conference', :start_date => today - 2.day, :end_date => today + 2.day)
  195 + event_in_day = build(Event, :name => 'Ze Birthday', :start_date => today)
196 196
197 profile.events << [event_in_range, event_in_day] 197 profile.events << [event_in_range, event_in_day]
198 198
@@ -205,9 +205,9 @@ class EventTest &lt; ActiveSupport::TestCase @@ -205,9 +205,9 @@ class EventTest &lt; ActiveSupport::TestCase
205 profile = fast_create(Profile) 205 profile = fast_create(Profile)
206 206
207 today = Date.today 207 today = Date.today
208 - event_in_range1 = Event.new(:name => 'Foswiki Conference', :start_date => today - 2.day, :end_date => today + 2.day)  
209 - event_in_range2 = Event.new(:name => 'Debian Conference', :start_date => today - 2.day, :end_date => today + 3.day)  
210 - event_out_of_range = Event.new(:name => 'Ze Birthday', :start_date => today - 5.day, :end_date => today - 3.day) 208 + event_in_range1 = build(Event, :name => 'Foswiki Conference', :start_date => today - 2.day, :end_date => today + 2.day)
  209 + event_in_range2 = build(Event, :name => 'Debian Conference', :start_date => today - 2.day, :end_date => today + 3.day)
  210 + event_out_of_range = build(Event, :name => 'Ze Birthday', :start_date => today - 5.day, :end_date => today - 3.day)
211 211
212 profile.events << [event_in_range1, event_in_range2, event_out_of_range] 212 profile.events << [event_in_range1, event_in_range2, event_out_of_range]
213 213