require 'test_helper'
class DocItemTest < ActiveSupport::TestCase
  should 'have id, title and text attributes' do
    item = DocItem.new
    item.id = 'my-title'; assert_equal 'my-title', item.id
    item.title = 'My title'; assert_equal 'My title', item.title
    item.text = 'My text'; assert_equal 'My text', item.text
  end
  should 'build object with attributes in hash' do
    item = DocItem.new(:title => 'The Shiny Documentation', :text => 'lorem ipsum', :language => 'tlh')
    assert_equal 'The Shiny Documentation', item.title
    assert_equal 'lorem ipsum', item.text
    assert_equal 'tlh', item.language
  end
  should 'use English as default language' do
    assert_equal 'en', DocItem.new.language
  end
  should 'expose processed text as HTML' do
    doc = DocItem.new(:text => 'this is the text')
    assert_equal 'this is the text', doc.html
  end
  should 'translate images' do
    doc = DocItem.new(:language => 'pt', :text => '
Look the image:

')
    File.stubs(:exist?).with(RAILS_ROOT + '/public/images/doc/myimage.pt.png').returns(false)
    assert_equal doc.text, doc.html
    File.stubs(:exist?).with(RAILS_ROOT + '/public/images/doc/myimage.pt.png').returns(true)
    assert_match(/
 'pt', :text => 'Look the image:

')
    # the image exists in both the system *and* on the theme
    File.stubs(:exist?).with(RAILS_ROOT + '/public/images/doc/myimage.pt.png').returns(true)
    File.stubs(:exist?).with(RAILS_ROOT + '/public/designs/themes/mytheme/images/doc/myimage.pt.png').returns(true)
    # the one in the theme must be used
    assert_match(/
 'pt', :text => 'Look the image:

')
    # the image has a translation in the system but not in the theme
    File.stubs(:exist?).with(RAILS_ROOT + '/public/images/doc/myimage.pt.png').returns(true)
    File.stubs(:exist?).with(RAILS_ROOT + '/public/designs/themes/mytheme/images/doc/myimage.en.png').returns(false)
    File.stubs(:exist?).with(RAILS_ROOT + '/public/designs/themes/mytheme/images/doc/myimage.pt.png').returns(false)
    # the one in the theme must be used
    assert_match(/
 'pt', :text => 'Look the image:

')
    # the image has no translation, but both system and theme provide an image
    File.stubs(:exist?).with(RAILS_ROOT + '/public/images/doc/myimage.en.png').returns(true)
    File.stubs(:exist?).with(RAILS_ROOT + '/public/images/doc/myimage.pt.png').returns(false)
    File.stubs(:exist?).with(RAILS_ROOT + '/public/designs/themes/mytheme/images/doc/myimage.en.png').returns(true)
    File.stubs(:exist?).with(RAILS_ROOT + '/public/designs/themes/mytheme/images/doc/myimage.pt.png').returns(false)
    # the one in the theme must be used
    assert_match(/