Module: Zena::Use::TestHelper

Includes:
Upload::UploadedFile
Included in:
Controller::TestCase, TestController, Zena::Unit::TestCase, View::TestCase
Defined in:
lib/zena/use/test_helper.rb

Instance Method Summary collapse

Instance Method Details

#err(obj) ⇒ Object

Show object’s errors



29
30
31
32
33
# File 'lib/zena/use/test_helper.rb', line 29

def err(obj)
  obj.errors.each_error do |er,msg|
    puts "[#{er}] #{msg}"
  end
end

#execute_after_commit!Object



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/zena/use/test_helper.rb', line 155

def execute_after_commit!
  Node.connection.instance_eval do
    # Simulate commit
    if actions = @after_commit
      @after_commit = nil
      actions.each do |block|
        block.call
      end
    end
  end
end

#file_path(filename, mode = 'full', content_id = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/zena/use/test_helper.rb', line 140

def file_path(filename, mode = 'full', content_id = nil)
  if content_id
    fname = filename.split('.').first
  else
    if content_id = document_contents_id(filename.to_sym)
      fname = filename.to_s.split('_').first
    else
      puts "#{filename.inspect} fixture not found in document_contents"
      return nil
    end
  end
  digest = Digest::SHA1.hexdigest(content_id.to_s)
  "#{SITES_ROOT}/test.host/data/#{mode}/#{digest[0..0]}/#{digest[1..1]}/#{digest[2..2]}/#{fname}"
end

#login(fixture, host = nil) ⇒ Object

Set visitor for unit testing



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zena/use/test_helper.rb', line 10

def (fixture, host = nil)
  user = fixture.kind_of?(Fixnum) ? User.find(fixture) : users(fixture)
  if host
    raise 'FIXME' unless host =~ /\./
    site = Site.setup_master(Site.find_by_host(host))
  else
    # Not an alias
    site = user.site
  end
  setup_visitor(user, site)
  user.ip = '10.0.0.44'
  $_test_site = site.name
  ::I18n.locale = user.lang
rescue => err
  puts "#{host.inspect}"
  puts err.backtrace
end

#preserving_files(path, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/zena/use/test_helper.rb', line 47

def preserving_files(path, &block)
  path = "/#{path}" unless path[0..0] == '/'
  if File.exist?("#{SITES_ROOT}#{path}")
    FileUtils::cp_r("#{SITES_ROOT}#{path}","#{SITES_ROOT}#{path}.bak")
    move_back = true
  else
    move_back = false
  end
  begin
    yield
  ensure
    FileUtils::rmtree("#{SITES_ROOT}#{path}")
    if move_back
      FileUtils::mv("#{SITES_ROOT}#{path}.bak","#{SITES_ROOT}#{path}")
    end
  end
end

#set_date(node_syms, opts = {}, site = 'zena') ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zena/use/test_helper.rb', line 35

def set_date(node_syms, opts = {}, site = 'zena')
  $_test_site = site
  fld = opts.delete(:fld) || 'log_at'
  if opts == {}
    date = Time.now
  else
    date = Time.now.advance(opts)
  end
  ids = node_syms.kind_of?(Array) ? node_syms.map{|node_sym| nodes_id(node_sym) } : [nodes_id(node_syms)]
  Node.connection.execute "UPDATE nodes SET #{fld} = '#{date.strftime('%Y-%m-%d %H:%M')}' WHERE id IN (#{ids.join(',')})"
end

#uploaded_archive(fname, filename = nil) ⇒ Object

TGZ helper



131
132
133
# File 'lib/zena/use/test_helper.rb', line 131

def uploaded_archive(fname, filename=nil)
  uploaded_fixture(fname, 'application/x-gzip', filename)
end

#uploaded_fixture(fname, content_type = "application/octet-stream", filename = nil) ⇒ Object

taken from manuals.rubyonrails.com/read/chapter/28#page237 with some modifications



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/zena/use/test_helper.rb', line 96

def uploaded_fixture(fname, content_type="application/octet-stream", filename=nil)
  path = File.join(FILE_FIXTURES_PATH, fname)
  filename ||= File.basename(path)
  # simulate small files with StringIO
  if File.stat(path).size < 1024
    # smaller then 1 Ko
    t = StringIO.new(File.read(path))
  else
    t = Tempfile.new(fname)
    FileUtils.copy_file(path, t.path)
  end
  uploaded_file(t, filename, content_type)
end

#uploaded_jpg(fname, filename = nil) ⇒ Object

JPEG helper



111
112
113
# File 'lib/zena/use/test_helper.rb', line 111

def uploaded_jpg(fname, filename=nil)
  uploaded_fixture(fname, 'image/jpeg', filename)
end

#uploaded_pdf(fname, filename = nil) ⇒ Object

Pdf helper



116
117
118
# File 'lib/zena/use/test_helper.rb', line 116

def uploaded_pdf(fname, filename=nil)
  uploaded_fixture(fname, 'application/pdf', filename)
end

#uploaded_png(fname, filename = nil) ⇒ Object

PNG helper



126
127
128
# File 'lib/zena/use/test_helper.rb', line 126

def uploaded_png(fname, filename=nil)
  uploaded_fixture(fname, 'image/png', filename)
end

#uploaded_text(fname, filename = nil) ⇒ Object

TEXT helper



121
122
123
# File 'lib/zena/use/test_helper.rb', line 121

def uploaded_text(fname, filename=nil)
  uploaded_fixture(fname, 'text/plain', filename)
end

#uploaded_zip(fname, filename = nil) ⇒ Object

ZIP helper



136
137
138
# File 'lib/zena/use/test_helper.rb', line 136

def uploaded_zip(fname, filename=nil)
  uploaded_fixture(fname, 'application/zip', filename)
end

#with_cachingObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zena/use/test_helper.rb', line 83

def with_caching
  @perform_caching_bak = ApplicationController.perform_caching
  ApplicationController.perform_caching = true
  Cache.perform_caching      = true
  CachedPage.perform_caching = true
  yield
ensure
  Cache.perform_caching = @perform_caching_bak
  CachedPage.perform_caching = @perform_caching_bak
  ApplicationController.perform_caching = @perform_caching_bak
end

#without_files(path, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/zena/use/test_helper.rb', line 65

def without_files(path, &block)
  path = "/#{path}" unless path[0..0] == '/'
  if File.exist?("#{SITES_ROOT}#{path}")
    FileUtils::mv("#{SITES_ROOT}#{path}","#{SITES_ROOT}#{path}.bak")
    move_back = true
  else
    move_back = false
  end
  begin
    yield
  ensure
    FileUtils::rmtree("#{SITES_ROOT}#{path}")
    if move_back
      FileUtils::mv("#{SITES_ROOT}#{path}.bak","#{SITES_ROOT}#{path}")
    end
  end
end