File: mock.rb

package info (click to toggle)
samizdat 0.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,576 kB
  • sloc: ruby: 7,776; xml: 899; sql: 897; sh: 67; makefile: 11
file content (184 lines) | stat: -rw-r--r-- 3,249 bytes parent folder | download | duplicates (4)
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
180
181
182
183
184
# Samizdat mock classes
#
#   Copyright (c) 2002-2011  Dmitry Borodaenko <angdraug@debian.org>
#
#   This program is free software.
#   You can distribute/modify this program under the terms of
#   the GNU General Public License version 3 or later.
#
# vim: et sw=2 sts=2 ts=8 tw=0

require 'samizdat'
require 'tzinfo'

class MockConfig < SiteConfig
  RDF = 'data/samizdat/rdf.yaml'
  DEFAULTS = 'data/samizdat/defaults.yaml'
  CONFIG = 'data/samizdat/config.yaml'

  def initialize
    @config = ConfigHash.new(load_yaml_file(RDF))
    @config.deep_update!(load_yaml_file(DEFAULTS))
    @config.deep_update!(load_yaml_file(CONFIG))
  end
end

class MockDb
  include Singleton

  def select_one(query, params={})
    nil
  end

  def fetch(query, params={})
    []
  end
end

class MockSite < Site
  include Singleton

  def initialize
    @name = 'samizdat'
    @config = MockConfig.new
    @cache = @local_cache = SiteCache.new(CacheSingleton.instance, @name)
    @shared_cache = CacheSingleton.instance
    @timezone = TZInfo::Timezone.get('Europe/London')
    @plugins = Plugins.new(self)
  end

  def content_dir
    ''
  end

  def upload_enabled?
    true
  end

  def db
    MockDb.instance
  end

  def rdf
    nil
  end
end

class MockSession
  def initialize
    @login = 'guest'
    @full_name = _(@login)
  end

  attr_accessor :member, :login, :full_name, :moderator

  def moderator?
    @moderator
  end
end

class MockRequest
  def initialize
    samizdat_bindtextdomain('C')
    @site = MockSite.instance
    @session = MockSession.new
    @moderate = false
    @options = {}
    @uri_prefix = ''
    @base = 'http://localhost/'
    @route = '/'
  end

  attr_accessor :site, :session, :moderate, :options, :uri_prefix, :base, :route

  def cookie(name)
    nil
  end

  def moderate?
    @moderate
  end
end

class MockMember
  def initialize(permissions = {})
    @id = 1
    @login = 'test'
    @full_name = 'Test'
    @permissions = permissions
  end

  attr_accessor :id, :login, :full_name

  def guest?
    'guest' == @login
  end

  def allowed_to?(action)
    @permissions[action]
  end

  def location
    @id.to_s
  end
end

class MockGuestMember < MockMember
  def initialize
    @id = nil
    @login = 'guest'
    @full_name = _(@login)
  end
end

class MockFromHash
  def initialize(params = {})
    @params = params
    @id = @params[:id]
  end

  attr_reader :id

  def method_missing(name, value = nil)
    if /=$/ =~ name.to_s
      @params[ name.to_s.sub(/=$/, '').to_sym ] = value
    else
      @params[name]
    end
  end
end

class MockMessage < MockFromHash
  def initialize(params = {})
    super
    @params = {
      :content => nil,
      :date => Time.now,
      :lang => nil,
      :creator => MockMember.new,
      :desc => nil,
      :parent => nil,
      :part_of_property => 'dct::isPartOf',
      :current => nil,
      :open => false,
      :nversions => 0,
      :translations => [],
      :nreplies => 0,
      :tags => [],
      :nrelated => 0,
      :moderation_log => []
    }.merge!(@params)

    @params[:content] ||= Content.new(MockSite.instance, @params[:id], @params[:creator].login)
  end

  def may_reply?
    true
  end
end

class MockUpload
  def path
    'mock_upload'
  end
end