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



25
26
27
28
29
# File 'lib/zena/use/test_helper.rb', line 25

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

#execute_after_commit!Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/zena/use/test_helper.rb', line 151

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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/zena/use/test_helper.rb', line 136

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, site_name = nil) ⇒ Object

Set visitor for unit testing



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

def (fixture, site_name = nil)
  user = users(fixture)
  if site_name
    site = Site.setup_master(Site.find_by_name(site_name))
  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
end

#preserving_files(path, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/zena/use/test_helper.rb', line 43

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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zena/use/test_helper.rb', line 31

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



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

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



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/zena/use/test_helper.rb', line 92

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



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

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

#uploaded_pdf(fname, filename = nil) ⇒ Object

Pdf helper



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

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

#uploaded_png(fname, filename = nil) ⇒ Object

PNG helper



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

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

#uploaded_text(fname, filename = nil) ⇒ Object

TEXT helper



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

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

#uploaded_zip(fname, filename = nil) ⇒ Object

ZIP helper



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

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

#with_cachingObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/zena/use/test_helper.rb', line 79

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/zena/use/test_helper.rb', line 61

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