#!/usr/bin/env ruby
# $Id: tc_site.rb,v 1.22 2004/04/22 14:47:49 eto Exp $
# Copyright (C) 2003-2004 Kouichirou Eto, All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
# You can redistribute it and/or modify it under the terms of the GNU GPL2.

$LOAD_PATH.unshift('../../lib')
$LOAD_PATH.unshift('../../compat')
require 'test/unit'
require 'qwik/site'
require 'qwik/ext_member'
require 'qwik/ext_siteconfig'

class TC_Farm < Test::Unit::TestCase
  def setup()
    @config = Qwik::Config.new(true)
    @farm = Qwik::Farm.new(@config)
  end

  def test_farm
    @dir = @config.data_dir.path+"test"
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
    assert_equal('qwik.jp', @config.default_hostname)
    assert_equal(true, @farm.exist?('www'))
    assert_instance_of(Qwik::Site, @farm.get_site('www'))
    assert_equal(false, @farm.exist?('test'))
    assert_equal(nil, @farm.get_site('test'))
    @dir.mkdir
    assert_equal(true, @farm.exist?('test'))
    assert_instance_of(Qwik::Site, @farm.get_site('test'))
  end

  def teardown
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
  end
end

class TC_Site < Test::Unit::TestCase
  def setup()
    @config = Qwik::Config.new(true)
    @config.data_dir = './'
    @dir = './test/'.path
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
    @dir.mkdir if !@dir.directory?
    @site = Qwik::Site.new(@config, 'test')
    @user = 'test@example.com'
  end

  def test_site
    assert_equal(false, @site.open?) # test_config
    page = @site.create('SiteConfig')
    page.store(":open:true")
    assert_equal(true, @site.open?)
    page.store(":open:false")
    assert_equal(false, @site.open?)
    @site.pages.delete('SiteConfig')
    assert_equal(false, @site.open?)
    assert_instance_of(String, @site.config[:theme])
    page.store(":theme:qwik")
    assert_equal('qwik.jp', @site.config[:sitename])

    assert_equal('qwik.jp/test', @site.title) # test_title
    page.store(":sitename:TestSite")
    assert_equal('TestSite', @site.config[:sitename])
    assert_equal('TestSite', @site.title)

    page = @site.create('TestPage')
    assert_equal('TestPage', page.get_title)
    assert_equal('TestPage - TestSite', @site.title('TestPage'))

    page = @site.create('1')
    assert_equal('1', page.get_title)
    assert_equal('1 - TestSite', @site.title('1'))
    page.store("* TestTitle")
    assert_equal('TestTitle - TestSite', @site.title('1'))

    # test_url
    assert_equal('http://qwik.jp/', @site.domainurl)
    assert_equal('http://qwik.jp/test/', @site.url)
    assert_equal('http://qwik.jp/test/1.html', @site.url('1'))
    assert_equal('test@qwik.jp', @site.ml)

    with_dir('www'){ # test_url_www
      site = Qwik::Site.new(@config, 'www')
      assert_equal('http://qwik.jp/', site.domainurl)
      assert_equal('http://qwik.jp/', site.url)
      assert_equal('www@qwik.jp', site.ml)
      assert_equal('qwik.jp', site.title)
    }

    with_dir('example.com'){ # test_url_with_dot
      site = Qwik::Site.new(@config, 'example.com')
      assert_equal('http://example.com/', site.domainurl)
      assert_equal('http://example.com/', site.url)
      assert_equal('info@example.com', site.ml)
      assert_equal('example.com', site.title)
    }
  end

  def teardown()
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
  end

  def with_dir(sitename)
    dir = ('./'+sitename+'/').path
    dir.rmtree if dir.directory?
    dir.rmdir  if dir.directory?
    dir.mkdir if !dir.directory?
    yield
    dir.rmtree if dir.directory?
    dir.rmdir  if dir.directory?
  end
end
