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



19
20
21
22
23
# File 'lib/zena/use/test_helper.rb', line 19

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

#execute_after_commit!Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/zena/use/test_helper.rb', line 145

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/zena/use/test_helper.rb', line 130

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) ⇒ Object

Set visitor for unit testing



10
11
12
13
14
15
16
# File 'lib/zena/use/test_helper.rb', line 10

def (fixture)
  user = users(fixture)
  Thread.current[:visitor] = user
  user.ip = '10.0.0.44'
  $_test_site = user.site.name
  ::I18n.locale = user.lang
end

#preserving_files(path, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zena/use/test_helper.rb', line 37

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



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/zena/use/test_helper.rb', line 25

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



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

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



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/zena/use/test_helper.rb', line 86

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



101
102
103
# File 'lib/zena/use/test_helper.rb', line 101

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

#uploaded_pdf(fname, filename = nil) ⇒ Object

Pdf helper



106
107
108
# File 'lib/zena/use/test_helper.rb', line 106

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

#uploaded_png(fname, filename = nil) ⇒ Object

PNG helper



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

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

#uploaded_text(fname, filename = nil) ⇒ Object

TEXT helper



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

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

#uploaded_zip(fname, filename = nil) ⇒ Object

ZIP helper



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

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

#with_cachingObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zena/use/test_helper.rb', line 73

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/zena/use/test_helper.rb', line 55

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