cirandas.net

ref: master

plugins/analytics/test/functional/content_viewer_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
require 'test_helper'

class ContentViewerControllerTest < ActionController::TestCase

  def setup
    @controller = ContentViewerController.new

    @environment = Environment.default
    @environment.enabled_plugins += ['AnalyticsPlugin']
    @environment.save!

    @user = create_user('testinguser').person
    login_as @user.identifier

    @community = build Community, identifier: 'testcomm', name: 'test'
    @community.analytics_settings.enabled = true
    @community.analytics_settings.anonymous = false
    @community.save!
    @community.add_member @user
  end

  should 'register page view correctly' do
    @request.env['HTTP_REFERER'] = 'http://google.com'
    first_url = 'http://test.host/testcomm'
    get :view_page, profile: @community.identifier, page: []
    assert_equal 1, @community.page_views.count
    assert_equal 1, @community.visits.count

    assert_equal @user, second_page_view.user
    assert second_page_view.request_duration > 0 and second_page_view.request_duration < 1

    first_page_view = @community.page_views.order(:id).first
    assert_equal @request.referer, first_page_view.referer_url
    assert_equal @user, first_page_view.user
    assert first_page_view.request_duration > 0 and first_page_view.request_duration < 1

    @request.env['HTTP_REFERER'] = first_url
    get :view_page, profile: @community.identifier, page: @community.articles.last.path.split('/')
    assert_equal 2, @community.page_views.count
    assert_equal 1, @community.visits.count

    second_page_view = @community.page_views.order(:id).last
    assert_equal first_page_view, second_page_view.referer_page_view

    # another visit, the referer is set but should be ignored because
    # the user didn't report to be on the page until now
    @request.env['HTTP_REFERER'] = first_url
    future = Time.now + 2*AnalyticsPlugin::TimeOnPageUpdateInterval
    Time.stubs(:now).returns(future)
    get :view_page, profile: @community.identifier, page: @community.articles.last.path.split('/')
    assert_equal 2, @community.visits.count
  end

end