#!/usr/bin/env ruby
# $Id: tc_extension.rb,v 1.11 2004/04/27 16:12:31 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/page'
require 'qwik/site'
require 'qwik/extension'
require 'qwik/wikitext'
require 'qwik/util'
require 'qwik/parser'
require 'qwik/formatter'

class TC_Plugin < Test::Unit::TestCase
  def setup()
    @config = Qwik::Config.new(true)
    @config.pass_file = './password.txt'
    @config.data_dir = './'
    @dir = './test/'.path
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
    @dir.mkdir if !@dir.directory?
    @memory = Qwik::ServerMemory.new(@config)

    @site = Qwik::Site.new(@config, 'test')
    @pagename = 'testpage'

    options = Hash.new('')
    options['memory']   = @memory
    options['pagename'] = @pagename
    options['site']     = @site
    @plugin = Qwik::Plugin.new(@config, options)
    @site.plugin = @plugin
  end

  def test_plugin
    # test_html
    assert_equal("<a href='t.html'>tt</a>", @plugin.a('t', 'tt').to_xhtml)
    assert_equal("<img src='t' alt='t' />", @plugin.img('t').to_xhtml)
    assert_equal("<img src='t' alt='m' />", @plugin.img('t', 'm').to_xhtml)
    assert_equal("<span style='font-size: smaller;'>a</span>", @plugin.small('a').to_xhtml)
    assert_equal("<span style='font-size: smaller;'>a</span>", @plugin.small(){'a'}.to_xhtml)
    assert_equal("<span style='font-size: smaller;'>a</span>", @plugin.small('a'){}.to_xhtml)
    assert_equal("<span style='font-size: smaller;'></span>", @plugin.small(){}.to_xhtml)
    assert_equal("<span style='font-size: smaller;'><img src='t' alt='t' /></span>", @plugin.small('{{img(t)}}').to_xhtml)
    assert_equal("<div style='text-align: center;'></div>", @plugin.center('a').to_xhtml)
    assert_equal("<div style='text-align: center;'>a</div>", @plugin.center(){'a'}.to_xhtml)
    assert_equal("<div style='text-align: center;'></div>", @plugin.center('a'){}.to_xhtml)
    assert_equal("<div style='text-align: center;'>y</div>", @plugin.center(){'y'}.to_xhtml)
    assert_equal("<div style='text-align: center;'><img src='t' alt='t' /></div>", @plugin.center(){'{{img(t)}}'}.to_xhtml)
    assert_equal("<div style='text-align: right;'><img src='t' alt='t' /></div>", @plugin.right(){'{{img(t)}}'}.to_xhtml)
    assert_equal("<div style='text-align: center;'>y</div>", @plugin.style_div('text-align: center;'){'y'}.to_xhtml)
    assert_equal(true, @plugin.style_valid?('text-align: center;'))
    assert_equal(false, @plugin.style_valid?('invalid@style'))
    assert_equal(false, @plugin.style_valid?('invalid.style'))
    assert_equal("", @plugin.style_div('invalid@style'){'y'}.to_xhtml)
    assert_equal("<br />", @plugin.br().to_xhtml)

    assert_equal("<div style='text-align: center;'>&lt;</div>", @plugin.style_div('text-align: center;'){'<'}.to_xhtml)

    # test_menu
    assert_equal(%Q|<a href='testpage.html?c=edit'>編集</a>|,
		 @plugin.menu('edit').to_xhtml)
    assert_equal('', @plugin.menu('nosuch'))

    # test_form()
    assert_equal("<div class='form'><form action='dest.html' method='POST'><input name='page' type='hidden' value='testpage' /><table class='form'><tr><td class='midashi'>k</td><td class='nakami'><span>v</span></td></tr></table></form></div>".chomp, @plugin.form('dest'){"|k|v|"}.to_xhtml)
    str =
"|mail|{{input(tomail,,must)}}|
|input your mail.|
||{{submit( GO )}}|"
    assert_equal("<div class='form'><form action='dest.html' method='POST'><input name='page' type='hidden' value='testpage' /><table class='form'><tr><td class='midashi'>mail</td><td class='nakami'><input name='tomail' size='must' value='' /></td></tr><tr><td class='msg' colspan='2'>input your mail.</td></tr><tr><td class='midashi'></td><td class='nakami'><input class='submit' type='submit' value=' GO ' /></td></tr></table></form></div>", @plugin.form('dest'){str}.to_xhtml)

    # test_common
#    assert_match(%r|<div><h3>[0-9-]+</h3><ul><li><a href='0.html'>0</a></li></ul></div>|, @plugin.recent(1).to_xhtml)

    # test_mail
    page = @site.pages.create_new
    page.store("* テスト\n{{mail(test@example.com)\nテストです。\n\n}}\n")
    xml = page.load.w.res(@site)
    assert_equal("<body><h2>テスト</h2>\n<div><h2>From: test@example.com</h2><p>テストです。</p></div></body>", xml.to_s)

    page.store("{{mail()\na\n}}\n")
    xml = page.load.w.res(@site)
    assert_equal("<body><div><h2>From:</h2><p>a</p></div></body>", xml.to_s)

    # test_text
    page.store("{{text(* あ)}}")
    xml = page.load.w.res(@site)
    assert_equal("<body><p><div><h2>あ</h2></div></p>\n</body>", xml.to_s)

    page.store("{{text()\n* い\n}}")
    xml = page.load.w.res(@site)
    assert_equal("<body><div><h2>い</h2></div></body>", xml.to_s)

    page.store("{{text(<)}}")
    xml = page.load.w.res(@site)
    assert_equal("<body><p><div><p>&lt;</p></div></p>\n</body>", xml.to_s)

    page.store("{{text()\n<\n}}")
    xml = page.load.w.res(@site)
    assert_equal("<body><div><p>&lt;</p></div></body>", xml.to_s)

    # test_include
    page2 = @site.pages.create_new
    page2.store("* う\n")
    key = page2.key
    page.store("{{include(#{key})}}")
    xml = page.load.w.res(@site)
    assert_equal("<body><p><div><h2>う</h2></div></p>\n</body>", xml.to_s)

    # test theme list
    page.store("{{theme_list}}")
    xml = page.load.w.res(@site)
    assert_match(/<body><p><ul><li>/, xml.to_s)
  end

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

require 'qwik/server'

class TC_PluginReal < Test::Unit::TestCase
  def setup
    @config = Qwik::Config.new(true)
    @config.pass_file = './password.txt'
    @config.data_dir = './'
    @dir = './test/'.path
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
    @dir.mkdir if !@dir.directory?
    @memory = Qwik::ServerMemory.new(@config)

    req = Qwik::Request.new(@config)
    res = Qwik::Response.new(@config)
    @session = Qwik::Session.new(@config, @memory, req, res)
    @session.req.parse_host_path('qwik.jp', '/test/')
    @session.req.cookies['user'] = 'test@example.com'
    @session.req.cookies['pass'] = 'E4268F8D'

    site = Qwik::Site.new(@config, 'test')
    site.member.add('test@example.com')

    @session.run
    @site = @session.site
    @plugin = @site.plugin
  end

  def test_plugin
    assert_match(/</, @session.res.body.to_xhtml(0))
  end

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