Changeset 317

Show
Ignore:
Timestamp:
2007-02-28 12:04:25 (2 years ago)
Author:
gaspard
Message:

Tests for Document, TextDocument?, Templates and Skin
Better content relation from Document to DocumentContent?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app/controllers/application.rb

    r315 r317  
    2626  end 
    2727   
     28  # TODO: test 
    2829  def template_url(opts={}) 
    2930    skin  = opts[:skin] || 'default' 
     
    9495  end 
    9596   
     97  # TODO: test 
    9698  def template_text_for_url(url) 
    9799    url = url[1..-1] # strip leading '/' 
     
    102104    end 
    103105    skin ||= secure(Skin) { Skin.find_by_name(skin_name) } 
    104     template = skin.template_for_path(url
    105     template.version.text 
     106    template = skin.template_for_path(url.join('/')
     107    template ? template.version.text : nil 
    106108  rescue 
    107109    return nil 
    108110  end 
    109111 
     112  # TODO: implement 
    110113  def template_url_for_asset(type,url) 
    111     return "/#{url}" # TODO IMPLEMENT 
    112     # current_template = @current_template || "/" 
    113     # we assume current_template is /projects/cleanWater for testing 
    114     current_template = 'projects/cleanWater' 
    115     path = current_template.split('/') + url.split('/') 
    116     doc = secure(Document) { Document.find_by_path(path)} 
    117     url = url_for(data_url(doc)) 
    118     if url =~ %r{http://test.host(.*)} 
    119       $1 
    120     else 
    121       url 
    122     end 
    123   end 
    124    
     114    # 1. find in current skin ? 
     115    # 2. find in corresponding public directory ? 
     116    case type 
     117    when :stylesheet 
     118    when :link 
     119    when :script 
     120    end 
     121    # 3. find in site ? 
     122  end 
     123   
     124  # TODO: implement 
    125125  def save_erb_to_url(template, template_url) 
    126126    "save '#{template_url}':[#{template}]" 
    127127  end 
    128    
    129    
    130    
    131    
    132    
    133    
    134    
    135    
    136    
    137    
    138    
    139128   
    140129  def page_not_found 
  • trunk/app/controllers/main_controller.rb

    r304 r317  
    1717  def show 
    1818    path = params[:path].dup 
    19     ref  = path.pop 
    20     if ref =~ /[a-zA-Z\-_]+([0-9]+)(\.|$)/ 
     19    if path[-1..-1] =~ /[a-zA-Z\-_]+([0-9]+)(\.|$)/ 
    2120      @node = secure(Node) { Node.find($1.to_i) } 
    2221    else 
    23       path << ref # unpop ref 
    24       @node = secure(Node) { Node.find_by_path(path) } 
     22      @node = secure(Node) { Node.find_by_path(path.join('/')) } 
    2523    end 
    26     if path == @node.basepath(true).split('/') 
     24    if path == @node.basepath(true) 
    2725      render_and_cache 
    2826    else 
  • trunk/app/models/document.rb

    r316 r317  
    33=end 
    44class Document < Page 
    5   before_validation :set_name 
    6   before_save       :update_content_name 
     5  before_validation :prepare_before_validation 
    76   
    87  class << self 
     
    1211     
    1312    alias o_create create 
    14      
    15     # TODO: test more (Templates and Skin tested without files) 
    1613    def create(hash) 
    1714      scope = self.scoped_methods[0] || {} 
     
    1916      if hash[:c_file] 
    2017        content_type = hash[:c_file].content_type 
    21         if Image.accept_content_type?(content_type) 
    22           klass = Image 
    23         elsif Template.accept_content_type?(content_type) 
    24           if hash[:parent_id] && Node.find(hash[:parent_id]).kind_of?(Skin) 
    25             klass = Template 
    26           else 
    27             klass = Skin 
    28           end 
    29         elsif TextDocument.accept_content_type?(content_type) 
    30           klass = TextDocument 
    31         else 
    32           klass = Document 
    33         end 
    34       elsif hash[:name] =~ /\.html$/ 
     18      elsif hash[:c_content_type] 
     19        content_type = hash[:c_content_type] 
     20      elsif hash[:name] =~ /^.*\.(\w+)$/ && types = EXT_TO_TYPE[$1] 
     21        content_type = types[0] 
     22      else 
     23        content_type = 'text/plain' 
     24      end 
     25      if Image.accept_content_type?(content_type) 
     26        klass = Image 
     27      elsif Template.accept_content_type?(content_type) 
    3528        if hash[:parent_id] && Node.find(hash[:parent_id]).kind_of?(Skin) 
    3629          klass = Template 
    37         else   
     30        else 
    3831          klass = Skin 
    3932        end 
     33      elsif TextDocument.accept_content_type?(content_type) 
     34        klass = TextDocument 
    4035      else 
    41         klass = TextDocument 
     36        klass = Document 
    4237      end 
    4338      klass.with_scope(scope) { klass.o_create(hash) } 
     
    4742  def c_file=(file) 
    4843    @file = file 
    49     # we call 'method missing' to do normal file setting on content 
    50     method_missing(:c_file=,file) 
    5144  end 
    5245   
     
    6659   
    6760  # Set name from filename 
    68   def set_name 
    69     return true unless new_record? 
    70     if self[:name] && self[:name] != "" 
    71       base = self[:name] 
    72     elsif @file 
    73       base = @file.original_filename 
     61  def prepare_before_validation 
     62    content = version.content 
     63    if new_record? 
     64      if self[:name] && self[:name] != "" 
     65        base = self[:name] 
     66      elsif @file 
     67        base = @file.original_filename 
     68      end 
     69      if base =~ /\./ 
     70        self[:name] = base.split('.')[0..-2].join('.') 
     71        ext  = base.split('.').last 
     72      end 
     73      content[:name] = self[:name] 
     74      content[:ext] = self[:ext] 
    7475    else 
    75       errors.add('name', 'cannot be empty') 
    76       return false 
     76      # when cannot use 'old' here as this record is not secured when spreading inheritance 
     77      if self[:name] != self.class.find(self[:id])[:name] && self[:name] && self[:name] != '' 
     78        # update all content names : 
     79        versions.each do |v| 
     80          content = v.content 
     81          content.name = self[:name] 
     82          content.save 
     83        end 
     84      end 
    7785    end 
    78     if base =~ /\./ 
    79       self[:name] = base.split('.')[0..-2].join('.') 
    80       c_ext  = base.split('.').last 
    81       c_name = self[:name] 
     86       
     87    if @file 
     88      # set file 
     89      content.file = @file 
    8290    end 
    83     return true 
    8491  end 
    8592 
    86   def update_content_name 
    87     # when cannot use 'old' here as this record is not secured when spreading inheritance 
    88     if !new_record? && (self[:name] != self.class.find(self[:id])[:name]) 
    89       # update all content names : 
    90       versions.each do |v| 
    91         content = v.content 
    92         content.name = self[:name] 
    93         content.save 
    94       end 
    95     end 
    96   end 
    9793  # This is a callback from acts_as_multiversioned 
    9894  def version_class 
  • trunk/app/models/document_content.rb

    r266 r317  
    22class DocumentContent < ActiveRecord::Base 
    33  belongs_to            :version 
    4    
    5   before_validation     :prepare_filename 
     4 
    65  validate              :valid_file 
    76  validates_presence_of :ext 
     
    4140  end 
    4241   
     42  def size=(s) 
     43    raise StandardError, "Size cannot be set. It is defined by the file size." 
     44  end 
     45   
    4346  def file=(aFile) 
    4447    @file = aFile 
     48     
    4549    self[:content_type] = @file.content_type.chomp 
    4650    if @file.kind_of?(StringIO) 
     
    4953      self[:size] = @file.stat.size 
    5054    end 
    51   end 
    52    
    53   def size=(s) 
    54     raise StandardError, "Size cannot be set. It is defined by the file size." 
     55    extension = self[:ext] || @file.original_filename.split('.').last 
     56    # is this extension valid ? 
     57    extensions = TYPE_TO_EXT[content_type] 
     58    if extensions 
     59      self[:ext] = extensions.include?(extension) ? extension : extensions[0] 
     60    else 
     61      self[:ext] = "???" 
     62    end 
    5563  end 
    5664   
     
    8997   
    9098  private 
    91  
    92   def prepare_filename 
    93     if new_record? 
    94       self[:name] = version.node.name 
    95       if @file 
    96         # set extension 
    97         ext  = self[:ext] || @file.original_filename.split('.').last 
    98         # is this extension valid ? 
    99         extensions = TYPE_TO_EXT[self[:content_type]] 
    100         if extensions 
    101           self[:ext] = extensions.include?(ext) ? ext : extensions[0] 
    102         else 
    103           self[:ext] = "???" 
    104         end 
    105        
    106         true 
    107       end 
    108     end 
    109   end 
    11099   
    111100  def valid_file 
  • trunk/app/models/document_version.rb

    r62 r317  
    22# is created, both the new and the old #DocumentVersion refer to the same file (#DocumentContent) 
    33class DocumentVersion < Version 
    4   before_validation_on_create :get_content 
    54  validates_presence_of       :content 
    65  def content_class 
    76    DocumentContent 
    87  end 
    9    
    10   private 
    11    
    12   def get_content 
    13     redaction_content unless content 
    14   end 
    158end 
  • trunk/app/models/node.rb

    r316 r317  
    7373    # Find an node by it's full path. Cache 'fullpath' if found. 
    7474    def find_by_path(path) 
    75       node = self.find_by_fullpath(path.join('/')
     75      node = self.find_by_fullpath(path
    7676      if node.nil? 
     77        path = path.split('/') 
    7778        last = path.pop 
    7879        Node.with_exclusive_scope do 
     
    146147  # Same as fullpath, but the path includes the root node. 
    147148  def rootpath 
    148     ZENA_ENV[:site_name] + "/" + fullpath 
     149    ZENA_ENV[:site_name] + (fullpath != "" ? "/#{fullpath}" : "") 
    149150  end 
    150151 
  • trunk/app/models/skin.rb

    r315 r317  
    22# of a site. 
    33class Skin < Template 
    4   # opts can be :mode and :klass 
     4   
    55  def template_url_for_name(template_name, helper) 
    66    if template_name == 'any' 
     
    2323   
    2424  def template_for_path(path) 
    25     Skin.logger.info "[#{name}] GET (#{path.inspect})" 
    2625    current = self 
    27     if path == ['any'] 
     26    if path == 'any' 
    2827      return current 
    29     else 
     28    else   
     29      path = path.split('/') 
    3030      while path != [] 
    3131        template_name = path.shift 
    32         Skin.logger.info "[#{name}] GET ('parent_id = #{current[:id]} AND name = '#{template_name}')" 
    33         begin 
    34           current = secure(Template) { Template.find(:first, :conditions=>["parent_id = ? AND name = ?", current[:id], template_name])} 
    35           Skin.logger.info "OK" 
    36         rescue ActiveRecord::RecordNotFound 
    37           Skin.logger.info "NOT FOUND" 
    38           break 
    39         end 
     32        current = secure(Template) { Template.find(:first, :conditions=>["parent_id = ? AND name = ?", current[:id], template_name])} 
    4033      end 
    41       if path == [] 
    42         current 
    43       else 
    44         nil 
    45       end 
     34      current 
    4635    end 
    4736  rescue ActiveRecord::RecordNotFound 
  • trunk/app/models/template.rb

    r316 r317  
    11class Template < TextDocument 
    2   before_validation :set_content_type 
     2  class << self 
     3    def accept_content_type?(content_type) 
     4      content_type == 'text/html' 
     5    end 
     6  end 
     7   
    38  # TODO: test 
    49  def sweep_cache 
     
    1924  private 
    2025   
    21   # TODO: test 
    22   def set_content_type 
    23     version.redaction_content.content_type = 'text/html' 
    24     version.content.ext  = 'html' 
    25     version.content.name = name 
     26  def prepare_before_validation 
     27    super 
     28    content = version.content 
     29    content[:content_type] = 'text/html' 
     30    content[:ext] = 'html' 
    2631  end 
    2732end 
  • trunk/app/models/text_document.rb

    r266 r317  
    2424   
    2525  private 
     26   
     27  def prepare_before_validation 
     28    super 
     29    content = version.content 
     30    content[:content_type] ||= 'text/plain' 
     31    content[:ext]  ||= 'txt' 
     32  end   
    2633  # This is a callback from acts_as_multiversioned 
    2734  def version_class 
  • trunk/app/models/version.rb

    r256 r317  
    7171      @content = content_class.find_by_version_id(self[:content_id]) 
    7272    else 
    73       content_class.find_by_version_id(self[:id]) 
     73      @content = content_class.find_by_version_id(self[:id]) 
     74    end 
     75    if @content 
     76      @content 
     77    else 
     78      # create new content 
     79      @content = content_class.new 
     80      @content.version = self 
     81      self[:content_id] = nil 
     82      @redaction_content = @content 
    7483    end 
    7584  end 
     
    8392      @redaction_content = @content 
    8493    elsif @content 
     94      # copy current content 
    8595      @content = content.clone 
    8696      @content.version = self 
     
    8898      @redaction_content = @content 
    8999    else 
     100      # create new content 
    90101      @content = content_class.new 
    91102      @content.version = self 
  • trunk/app/views/templates

    • Property svn:ignore set to compiled
  • trunk/lib/parser/lib/parser.rb

    r315 r317  
    33  require File.join(File.dirname(__FILE__) , 'rules', file) 
    44end 
     5module ParserModule 
     6  class DummyHelper 
     7    def initialize(strings = {}) 
     8      @strings = strings 
     9    end 
     10 
     11    def template_text_for_url(url) 
     12      url = url[1..-1] # strip leading '/' 
     13      url = url.gsub('/','_') 
     14      if test = @strings[url] 
     15        test['src'] 
     16      else 
     17        nil 
     18      end 
     19    end 
     20 
     21    def template_url_for_asset(type,url) 
     22      "/test_#{type}/#{url}" 
     23    end 
     24 
     25    def method_missing(sym, *args) 
     26      arguments = args.map do |arg| 
     27        if arg.kind_of?(Hash) 
     28          res = [] 
     29          arg.each do |k,v| 
     30            unless v.nil? 
     31              res << "#{k}:#{v.inspect.gsub(/'|"/, "|")}" 
     32            end 
     33          end 
     34          res.sort.join(' ') 
     35        else 
     36          arg.inspect.gsub(/'|"/, "|") 
     37        end 
     38      end 
     39      res = "[#{sym} #{arguments.join(' ')}]" 
     40    end 
     41  end 
     42end 
     43 
    544class Parser 
    645  attr_accessor :text, :method 
     
    1655    end 
    1756    def new_with_url(url, opts={}) 
    18       helper = opts[:helper] || Zafu::DummyHelper 
     57      helper = opts[:helper] || ParserModule::DummyHelper.new 
    1958      text, absolute_url = self.find_template_text(url,helper) 
    2059      current_folder = absolute_url ? absolute_url.split('/')[0..-2].join('/') : '/' 
  • trunk/lib/parser/lib/rules/zena.rb

    r315 r317  
    424424      else 
    425425        select = select[1..-1] if select[0..0] == '/' 
    426         do_var("secure(Node) { Node.find_by_path(#{select.split('/').inspect})} rescue nil") 
     426        do_var("secure(Node) { Node.find_by_path(#{select.inspect})} rescue nil") 
    427427      end 
    428428    end 
  • trunk/lib/parser/test/testhelp.rb

    r313 r317  
    22require 'yaml' 
    33require File.join(File.dirname(__FILE__) , '..', 'lib', 'parser') 
    4  
    5 class DummyHelper 
    6   def initialize(strings = {}) 
    7     @strings = strings 
    8   end 
    9    
    10   def template_text_for_url(url) 
    11     url = url[1..-1] # strip leading '/' 
    12     url = url.gsub('/','_') 
    13     if test = @strings[url] 
    14       test['src'] 
    15     else 
    16       nil 
    17     end 
    18   end 
    19    
    20   def template_url_for_asset(type,url) 
    21     "/test_#{type}/#{url}" 
    22   end 
    23    
    24   def method_missing(sym, *args) 
    25     arguments = args.map do |arg| 
    26       if arg.kind_of?(Hash) 
    27         res = [] 
    28         arg.each do |k,v| 
    29           unless v.nil? 
    30             res << "#{k}:#{v.inspect.gsub(/'|"/, "|")}" 
    31           end 
    32         end 
    33         res.sort.join(' ') 
    34       else 
    35         arg.inspect.gsub(/'|"/, "|") 
    36       end 
    37     end 
    38     res = "[#{sym} #{arguments.join(' ')}]" 
    39   end 
    40 end 
    414 
    425class Test::Unit::TestCase 
  • trunk/test/helpers/testhelp.rb

    r282 r317  
    6060    end 
    6161  end 
    62  
    63   def template_url_for_asset(type,url) 
    64     # current_template = @current_template || "/" 
    65     # we assume current_template is /projects/cleanWater for testing 
    66     current_template = 'projects/cleanWater' 
    67     path = current_template.split('/') + url.split('/') 
    68     doc = secure(Document) { Document.find_by_path(path)} 
    69     url = url_for(data_url(doc)) 
    70     if url =~ %r{http://test.host(.*)} 
    71       $1 
    72     else 
    73       url 
    74     end 
    75   end 
    7662   
    77   def save_erb_to_url(template, template_url) 
    78     return '' 
    79     "save '#{template_url}':[#{template}]" 
    80   end 
    8163end 
    8264 
  • trunk/test/unit/document_test.rb

    r140 r317  
    88    assert Document.read_inheritable_attribute(:validate_on_create).include?(:node_on_create) 
    99    assert Document.read_inheritable_attribute(:validate_on_update).include?(:node_on_update) 
    10     assert Document.read_inheritable_attribute(:before_validation).include?(:set_name) 
    11     assert Document.read_inheritable_attribute(:before_save).include?(:update_content_name) 
     10    assert Document.read_inheritable_attribute(:before_validation).include?(:prepare_before_validation) 
    1211  end 
    1312   
     
    7776  def get_with_full_path 
    7877    test_visitor(:tiger) 
    79     doc = secure(Document) { Document.find_by_path( visitor_id, visitor_groups, lang, "/projects/cleanWater/water.pdf") } 
     78    doc = secure(Document) { Document.find_by_path("/projects/cleanWater/water.pdf") } 
    8079    assert_kind_of Document, doc 
    8180    assert_equal "/projects/cleanWater/water.pdf", doc.fullpath 
  • trunk/test/unit/node_test.rb

    r299 r317  
    1717    node = nodes(:wiki) 
    1818    assert_nil node[:fullpath] 
    19     node = secure(Node) { Node.find_by_path(['projects', 'wiki']) } 
     19    node = secure(Node) { Node.find_by_path('projects/wiki') } 
    2020    assert_equal 'projects/wiki', node.fullpath 
    21     node = secure(Node) { Node.find_by_path(['projects', 'wiki']) } 
     21    node = secure(Node) { Node.find_by_path('projects/wiki') } 
    2222    assert_equal 'projects/wiki', node[:fullpath] 
    2323  end 
     
    4242    assert_nothing_raised { node = secure(Node) { nodes(:status) } } 
    4343    assert_kind_of Node, node 
    44     assert_raises (ActiveRecord::RecordNotFound) { node = secure(Node) { Node.find_by_path(['people', 'ant']) } } 
    45     assert_nothing_raised { node = secure(Node) { Node.find_by_path(['people', 'ant', 'status'])}} 
     44    assert_raises (ActiveRecord::RecordNotFound) { node = secure(Node) { Node.find_by_path('people/ant') } } 
     45    assert_nothing_raised { node = secure(Node) { Node.find_by_path('people/ant/status')}} 
    4646  end 
    4747   
     
    4949    test_visitor(:ant) 
    5050    node = secure(Node) { nodes(:status) } 
    51     assert_equal ['zena', 'projects', 'cleanWater', 'status'], node.rootpath 
     51    assert_equal 'zena/projects/cleanWater/status', node.rootpath 
    5252    node = secure(Node) { nodes(:zena) } 
    53     assert_equal ['zena'], node.rootpath 
     53    assert_equal 'zena', node.rootpath 
    5454  end 
    5555   
     
    285285   
    286286  def test_project 
    287     assert_equal nodes(:zena).id, secure(Node) { nodes(:wiki) }.project.id 
     287    assert_equal nodes(:zena).id, secure(Node) { nodes(:people) }.project.id 
    288288  end 
    289289   
     
    355355  def test_secure_find_by_path 
    356356    test_visitor(:tiger) 
    357     node = secure(Node) { Node.find_by_path(['projects', 'secret']) } 
     357    node = secure(Node) { Node.find_by_path('projects/secret') } 
    358358    assert_kind_of Node, node 
    359359    test_visitor(:ant) 
    360     assert_raise(ActiveRecord::RecordNotFound) { node = secure(Node) { Node.find_by_path(['projects', 'secret']) }} 
     360    assert_raise(ActiveRecord::RecordNotFound) { node = secure(Node) { Node.find_by_path('projects/secret') }} 
    361361  end 
    362362   
     
    696696    assert_equal 1, node.comments.size 
    697697    ZENA_ENV[:allow_anonymous_comments] = false 
    698     assert node.can_comment?, "Anonymous cannot comment." 
     698    assert !node.can_comment?, "Anonymous cannot comment." 
    699699    ZENA_ENV[:allow_anonymous_comments] = true 
    700700    assert node.can_comment?, "Anonymous can comment." 
     
    729729    test_visitor(:ant) 
    730730    node = secure(Node) { nodes(:status) } 
    731     res = {:conditions=>["project_id = ? AND kpath NOT LIKE 'NPDI%'", 11], :order=>"name ASC"} 
     731    res = {:conditions=>["(project_id = ?) AND (kpath NOT LIKE 'NPDI%')", 11], :order=>"name ASC"} 
    732732    assert_equal res, node.relation_options({:from=>'project'}, "kpath NOT LIKE 'NPDI%'") 
    733     res = {:conditions=>["parent_id = ? AND kpath NOT LIKE 'NPDI%'", 12], :order=>"name ASC"} 
     733    res = {:conditions=>["(parent_id = ?) AND (kpath NOT LIKE 'NPDI%')", 12], :order=>"name ASC"} 
    734734    assert_equal res, node.relation_options({}, "kpath NOT LIKE 'NPDI%'") 
    735735  end 
  • trunk/test/unit/skin_test.rb

    r280 r317  
    44  include ZenaTestUnit 
    55 
    6   # Replace this with your real tests. 
    7   def test_truth 
    8     assert true 
     6  def test_template_url_for_name 
     7    without_files('app/views/templates/compiled') do 
     8      skin = secure(Skin) { Skin.find_by_name('wiki')} 
     9      assert_kind_of Skin, skin 
     10      path = File.join(RAILS_ROOT, 'app', 'views', 'templates', 'compiled', 'wiki', 'any_en.rhtml') 
     11      assert !File.exist?(path), "File does not exist" 
     12      assert_equal "/templates/compiled/wiki/any_en", skin.template_url_for_name('any', nil) 
     13      assert File.exist?(path), "File exists" 
     14      path = File.join(RAILS_ROOT, 'app', 'views', 'templates', 'compiled', 'wiki', 'layout_en.rhtml') 
     15      assert !File.exist?(path), "File does not exist" 
     16      assert_equal "/templates/compiled/wiki/layout_en", skin.template_url_for_name('layout', nil) 
     17      assert File.exist?(path), "File exists" 
     18      assert_equal nil, skin.template_url_for_name('bad', nil) 
     19    end 
     20  end 
     21   
     22  def test_template_for_path 
     23    skin = secure(Skin) { Skin.find_by_name('wiki')} 
     24    assert_kind_of Skin, skin 
     25    tmpl = skin.template_for_path('any') 
     26    assert_equal tmpl, skin 
     27    tmpl = skin.template_for_path('layout') 
     28    assert_equal nodes_id(:wiki_layout), tmpl[:id] 
     29    tmpl = skin.template_for_path('bad') 
     30    assert_nil tmpl 
    931  end 
    1032end 
  • trunk/test/unit/template_test.rb

    r313 r317  
    22 
    33class TemplateTest < Test::Unit::TestCase 
    4  
    5   # Replace this with your real tests. 
    6   def test_truth 
    7     assert true 
     4  include ZenaTestUnit 
     5   
     6  def test_create_simplest 
     7    test_visitor(:tiger) 
     8    doc = secure(Document) { Document.create(:parent_id=>nodes_id(:cleanWater), :name=>'skiny.html')} 
     9    assert_kind_of Skin, doc 
     10    assert !doc.new_record?, "Not a new record" 
     11    assert_equal 'text/html', doc.c_content_type 
     12    assert_equal 'html', doc.c_ext 
     13    sub = secure(Document) { Document.create(:parent_id=>doc[:id], :name=>'sub', :c_content_type=>'text/html')} 
     14    assert_kind_of Template, sub 
     15    assert !sub.kind_of?(Skin) 
     16    assert !sub.new_record?, "Not a new record" 
     17  end 
     18   
     19  def test_create_with_file 
     20    test_visitor(:tiger) 
     21    doc = secure(Document) { Document.create(:parent_id=>nodes_id(:cleanWater), :name=>'skiny',  
     22      :c_file=>uploaded_file('some.txt', content_type="text/html", 'smoke'))} 
     23    assert_kind_of Skin, doc 
     24    assert !doc.new_record?, "Not a new record" 
     25    assert_equal 'text/html', doc.c_content_type 
     26    assert_equal 'html', doc.c_ext 
     27    assert_equal 'skiny.html', doc.c_filename 
     28    sub = secure(Document) { Document.create(:parent_id=>doc[:id], :name=>'sub.html')} 
     29    assert_kind_of Template, sub 
     30    assert !sub.kind_of?(Skin) 
     31    assert !sub.new_record?, "Not a new record" 
    832  end 
    933end 
  • trunk/test/unit/text_document_test.rb

    r316 r317  
    33class TextDocumentTest < Test::Unit::TestCase 
    44  include ZenaTestUnit 
    5  
    6   # Replace this with your real tests. 
     5   
     6  def test_callbacks_for_documents 
     7    assert Node.read_inheritable_attribute(:before_validation).include?(:secure_before_validation) 
     8    assert TextDocument.read_inheritable_attribute(:validate_on_create).include?(:node_on_create) 
     9    assert TextDocument.read_inheritable_attribute(:validate_on_update).include?(:node_on_update) 
     10    assert TextDocument.read_inheritable_attribute(:before_validation).include?(:prepare_before_validation) 
     11  end 
     12   
    713  def test_create_simplest 
    814    test_visitor(:tiger) 
    915    doc = secure(Document) { Document.create(:parent_id=>nodes_id(:cleanWater), :name=>'skiny')} 
    10     assert_kind_of TextDocument, doc 
     16    assert_equal TextDocument, doc.class 
     17    err doc 
    1118    assert !doc.new_record?, "Not a new record" 
    1219  end 
    1320   
    14   def test_create_template 
    15     test_visitor(:tiger) 
    16     doc = secure(Document) { Document.create(:parent_id=>nodes_id(:cleanWater), :name=>'skiny.html')} 
    17     assert_kind_of Skin, doc 
    18     assert !doc.new_record?, "Not a new record" 
    19     assert_equal 'text/html', doc.c_content_type 
    20     assert_equal 'html', doc.c_ext 
    21     sub = secure(Document) { Document.create(:parent_id=>doc[:id], :name=>'sub.html')} 
    22     assert_kind_of Template, sub 
    23     assert !sub.kind_of?(Skin) 
    24     assert !sub.new_record?, "Not a new record" 
     21  def test_create_with_file 
     22    next_id = Version.find(:first, :order=>"id DESC")[:id] + 1 
     23    doc = secure(Document) { Document.create( :parent_id=>nodes_id(:cleanWater), 
     24                                              :v_title => 'My new project', 
     25                                              :c_file => uploaded_text('some.txt', 'stupid.jpg') ) } 
     26    assert_equal TextDocument, doc.class 
     27    assert !File.exist?(doc.c_filepath), "No file created" 
     28    assert_equal 'txt', doc.c_ext 
     29    assert_equal 'text/plain', doc.c_content_type 
     30    assert_equal 'stupid.txt', doc.c_filename 
     31  end 
     32   
     33  def test_content_lang 
     34    doc = secure(Document) { Document.create( :parent_id=>nodes_id(:cleanWater), :v_title => 'super script',  
     35                                              :c_content_type => 'text/x-ruby-script')} 
     36    assert_equal TextDocument, doc.class 
     37    assert_equal 'ruby', doc.content_lang 
     38     
     39    doc = secure(Document) { Document.create( :parent_id=>nodes_id(:cleanWater), :v_title => 'super script',  
     40                                              :c_content_type => 'text/html')} 
     41    assert_equal Skin, doc.class 
     42    assert_equal 'zafu', doc.content_lang 
    2543  end 
    2644end