1 require File.dirname(__FILE__) + '/test_helper'
3 class SitealizerController
4 def rescue_action(e) raise e end
5 SA_CONFIG = {'sitealizer' => {'username' => 'test', 'password' => 'test', 'access_type' => 'public'}}
8 class SitealizerControllerTest < Test::Unit::TestCase
9 set_fixture_class :sitealizer => 'SiteTracker'
13 @controller = SitealizerController.new
14 @request = ActionController::TestRequest.new
15 @response = ActionController::TestResponse.new
20 assert_template 'login'
22 post :login, :username => 'test', :password => 'test'
23 assert_response :redirect
24 assert_redirected_to :action => 'index'
25 assert_not_nil @response.cookies["sitealizer"][0]
29 post :login, :username => 'me', :password => 'me'
30 assert_not_nil flash[:notice]
31 assert_template 'login'
36 assert @response.cookies["sitealizer"].empty?
37 assert_response :redirect
38 assert_redirected_to :action => 'login'
43 assert_template 'index'
44 get :index, :year => '2006', :month => '03'
45 assert_template 'index'
50 assert_template 'menu'
51 assert_not_nil assigns(:months)
52 assert_not_nil assigns(:years)
57 assert_equal 9, assigns(:total_hits)
58 assert_equal 7, assigns(:unique_hits)
59 assert_equal 9, assigns(:year_hits)
60 assert_equal 7, assigns(:year_uniq)
61 assert_equal [1, 2, 3, "Mar 01 2007"], assigns(:daily_hits).first
62 assert_equal ["Mar 2007", 7, 9, "March 2007"], assigns(:month_hits)[2]
63 assert_template 'summary'
66 def test_summary_no_records
67 get :summary, :year => '2006', :month => '01'
68 [:total_hits, :unique_hits, :year_hits, :year_uniq].each do |var|
69 assert_equal 0, assigns(var)
72 expected = [[1, 0, 0, "Jan 01 2006"], [2, 0, 0, "Jan 02 2006"], [3, 0, 0, "Jan 03 2006"],
73 [4, 0, 0, "Jan 04 2006"], [5, 0, 0, "Jan 05 2006"], [6, 0, 0, "Jan 06 2006"],
74 [7, 0, 0, "Jan 07 2006"], [8, 0, 0, "Jan 08 2006"], [9, 0, 0, "Jan 09 2006"],
75 [10, 0, 0, "Jan 10 2006"], [11, 0, 0, "Jan 11 2006"], [12, 0, 0, "Jan 12 2006"],
76 [13, 0, 0, "Jan 13 2006"], [14, 0, 0, "Jan 14 2006"], [15, 0, 0, "Jan 15 2006"],
77 [16, 0, 0, "Jan 16 2006"], [17, 0, 0, "Jan 17 2006"], [18, 0, 0, "Jan 18 2006"],
78 [19, 0, 0, "Jan 19 2006"], [20, 0, 0, "Jan 20 2006"], [21, 0, 0, "Jan 21 2006"],
79 [22, 0, 0, "Jan 22 2006"], [23, 0, 0, "Jan 23 2006"], [24, 0, 0, "Jan 24 2006"],
80 [25, 0, 0, "Jan 25 2006"], [26, 0, 0, "Jan 26 2006"], [27, 0, 0, "Jan 27 2006"],
81 [28, 0, 0, "Jan 28 2006"], [29, 0, 0, "Jan 29 2006"], [30, 0, 0, "Jan 30 2006"],
82 [31, 0, 0, "Jan 31 2006"]]
83 assert_equal expected, assigns(:daily_hits)
85 expected = [["Jan 2006", 0, 0, "January 2006"], ["Feb 2006", 0, 0, "February 2006"],
86 ["Mar 2006", 0, 0, "March 2006"], ["Apr 2006", 0, 0, "April 2006"],
87 ["May 2006", 0, 0, "May 2006"], ["Jun 2006", 0, 0, "June 2006"],
88 ["Jul 2006", 0, 0, "July 2006"], ["Aug 2006", 0, 0, "August 2006"],
89 ["Sep 2006", 0, 0, "September 2006"], ["Oct 2006", 0, 0, "October 2006"],
90 ["Nov 2006", 0, 0, "November 2006"], ["Dec 2006", 0, 0, "December 2006"]]
91 assert_equal expected, assigns(:month_hits)
93 assert_template 'summary'
98 assert_equal 9, assigns(:total_hits)
101 assert_equal ["00", 3, 4], assigns(:hourly_hits).first
102 assert_template 'hourly_stats'
105 def test_hourly_stats_no_records
106 get :hourly_stats, :year => '2006', :month => '01'
107 expected = [["00", 0, 0], ["01", 0, 0], ["02", 0, 0], ["03", 0, 0], ["04", 0, 0], ["05", 0, 0],
108 ["06", 0, 0], ["07", 0, 0], ["08", 0, 0], ["09", 0, 0], ["10", 0, 0], ["11", 0, 0],
109 ["12", 0, 0], ["13", 0, 0], ["14", 0, 0], ["15", 0, 0], ["16", 0, 0], ["17", 0, 0],
110 ["18", 0, 0], ["19", 0, 0], ["20", 0, 0], ["21", 0, 0], ["22", 0, 0], ["23", 0, 0]]
111 assert_equal expected, assigns(:hourly_hits)
112 assert_template 'hourly_stats'
115 def test_search_stats
118 assert_equal 2, assigns(:keywords).size
119 expected = [{:total => 3, :query => "sitealizer"},{:total => 1, :query => "sitealizer rails plugin"}]
120 expected.each do |term|
121 assert_equal term, assigns(:keywords)[expected.index(term)]
124 assert_equal 2, assigns(:robots).size
125 expected = [{:total => 2, :name => "Googlebot"},{:total => 1, :name => "MSNBot"}]
126 expected.each do |robot|
127 assert_equal robot, assigns(:robots)[expected.index(robot)]
130 assert_equal 3, assigns(:engines).size
131 expected = [{:total=>2, :domain=>"search.aol.com"}, {:total=>1, :domain=>"www.altavista.com"},
132 {:total=>1, :domain=>"search.yahoo.com"}]
133 expected.each do |engine|
134 assert_equal engine, assigns(:engines)[expected.index(engine)]
137 assert_template 'search_stats'
140 def test_search_stats_no_records
141 get :search_stats, :year => '2006', :month => '01'
142 [:keywords, :engines, :robots].each do |var|
143 assert assigns(var).empty?
145 assert_template 'search_stats'
148 def test_visitor_info
151 assert_equal 4, assigns(:browsers).size
152 expected = ["Netscape","Other","MSIE","Safari"]
153 expected.each do |browser|
154 assert_equal browser, assigns(:browsers)[expected.index(browser)][:browser][:type]
157 assert_equal 3, assigns(:platforms).size
158 expected = ["Windows","Other","Macintosh"]
159 expected.each do |platform|
160 assert_equal platform, assigns(:platforms)[expected.index(platform)][:name]
163 assert_equal 7, assigns(:hosts).size
164 expected = ["41.41.41.41","1.1.1.1","123.4.5.678","31.31.31.31","51.51.51.51","3.3.3.3","2.2.2.2"]
165 expected.each do |host|
166 assert_equal host, assigns(:hosts)[expected.index(host)]["ip"]
169 assert_equal 4, assigns(:languages).size
170 expected = ["en",nil,"fr-fr","pt-pt"]
171 expected.each do |language|
172 assert_equal language, assigns(:languages)[expected.index(language)][:language]
175 assert_template 'visitor_info'
178 def test_visitor_info_no_records
179 get :visitor_info, :year => '2006', :month => '01'
180 assert_equal 0, assigns(:total_hits)
181 [:browsers, :platforms, :hosts, :languages].each do |var|
182 assert assigns(var).empty?
184 assert_template 'visitor_info'
187 def test_referrer_stats
189 assert_not_nil assigns(:page_urls)
190 assert_equal 2, assigns(:page_urls).size
191 [[0,"/"],[1,"/account"]].each do |page|
192 assert_equal page[1], assigns(:page_urls)[page[0]].path
194 assert_not_nil assigns(:referers)
195 assert_equal 5, assigns(:referers).size
196 assert_template 'referrer_stats'
199 def test_referrer_stats_no_records
200 get :referrer_stats, :year => '2007', :month => '01'
201 assert_equal 0, assigns(:total_hits)
202 [:page_urls, :referers].each do |var|
203 assert assigns(var).empty?
205 assert_template 'referrer_stats'