#!/usr/bin/env ruby
# $Id: tc_command.rb,v 1.26 2004/04/23 17:05:47 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/server'

class TC_Command < Test::Unit::TestCase
  def setup
    @config = Qwik::Config.new(true)
    @config.test = 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)
    @req.cookies['user'] = 'test@example.com'
    @req.cookies['pass'] = 'E4268F8D'
  end

  def test_command
    @req.parse_host_path('qwik.jp', '/test/1.html')

    site = Qwik::Site.new(@config, 'test')
    site.member.add('test@example.com')
    page = site.create('1')
    @pagestr = <<"EOT"
* テスト
です。
** エイチ3
です。
* エイチ2
です。
EOT
    page.store(@pagestr)
    page = site['SideMenu']
    page.store("* サイド\nです。[[1]]")

    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
#    puts xml.to_xhtml(0).u8tosjis
#    assert_equal('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">', xml.doctype.to_s)
    assert_equal("テスト - qwik.jp/test", xml.elements['/html/head/title'].text)
    assert_equal("テスト - qwik.jp/test", xml.elements['//title'].text)
   assert_equal("サイド", xml.elements['//[@class="sidebar"]/div/h2'].text)
#    e = xml.elements['//[@class="sidebar"]//a']
#    puts e.to_xhtml(0).u8tosjis
    assert_equal("テスト", xml.elements['//[@class="sidebar"]//a'].text)
    assert_equal("テスト", xml.elements['//[@class="main"]/h1'].text)
    assert_equal("編集", xml.elements['//[@class="adminmenu"]//a'].text)
    assert_equal("テスト", xml.elements['//[@class="day"]/h2'].text)
    assert_equal("エイチ3", xml.elements['//[@class="body"]//h3'].text)
    assert_equal("エイチ2", xml.elements['//[@class="day"][2]/h2'].text)
    assert_equal("です。", xml.elements['//[@class="day"][2]//p'].text)
    assert_equal("テスト", xml.elements['//h1'].text)
    assert_equal("テスト", xml.elements['//h2'].text)
    assert_equal("エイチ3", xml.elements['//h3'].text)
#    e = xml.elements['//[@class="caption"]/div/p/div']
#    puts e.to_xhtml(0).u8tosjis
    assert_match(/^last modified/, xml.elements['//[@class="caption"]/div/p/div'].text)

    @req.query['c'] = 'edit'
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
#    e = xml.elements['//head']
#    puts e.to_xhtml(0).u8tosjis
    assert_equal("テスト - qwik.jp/test", xml.elements['//title'].text)
    assert_equal("テスト - qwik.jp/test", xml.elements['//h1'].text)
    assert_equal(@pagestr, xml.elements['//textarea'].text)

    @req.query['contents'] = @pagestr+"* エイチ2の2\nです。\n"
    @req.query['c'] = 'save'
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
    assert_equal("ページを保存しました", xml.elements['//h1'].text)
    assert_equal("ページを保存しました", xml.elements['//title'].text)
    # If debug mode, Location redirect is disabled.
    #res = @session.res
    #assert_equal(302, res.status)
    #assert_equal('1.html', res['Location'])

    @req.query['c'] = nil
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
    assert_equal("テスト", xml.elements['//h1'].text)
    assert_equal("エイチ2の2", xml.elements['//[@class="day"][3]/h2'].text)
#    e = xml.elements['//h2(2)']
#    puts e.to_xhtml(0).u8tosjis

    site = @session.site
    str = site['1'].load
    @req.query['contents'] = str+"[[新規テストページ]]です。\n"
    @req.query['c'] = 'save'
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
    assert_equal("ページを保存しました", xml.elements['//h1'].text)
    assert_equal("ページを保存しました", xml.elements['//title'].text)

    @req.query['c'] = nil
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
    assert_equal("テスト", xml.elements['//h1'].text)
    assert_equal("エイチ2の2", xml.elements['//[@class="day"][3]/h2'].text)
#    assert_equal("エイチ2の2", xml.elements['//a'].text)

    page = site['SideMenu']
    page.store(<<"EOT")
* サイド
です。
{{recent(1)}}
EOT
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
    assert_equal("サイド", xml.elements['//[@class="sidebar"]/div/h2'].text)
#    puts e.to_xhtml(0).u8tosjis
#    assert_equal("テスト", xml.elements['//[@class="sidebar"]//a'].text)
#    assert_equal("SideMenu", xml.elements['//[@class="sidebar"]//a'].text)

#    @req.parse_host_path('qwik.jp', '/test/_textformatsimple.html')
    @req.parse_host_path('qwik.jp', '/test/TextFormatSimple.html')
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
#    assert_match(/書式一覧/, xml.elements['//h1'].text)
    assert_equal("TextFormatSimple", xml.elements['//h1'].text)
    str = xml.to_xhtml(0).u8tosjis
  end

  def test_save
    @req.parse_host_path('qwik.jp', '/test/')
    @session = Qwik::Session.new(@config, @memory, @req, @res)

    @session.run
    assert_match(/ですか/, @res.body.to_xhtml(0))

    @site = @session.site
    @site.member.add('test@example.com')
    assert_equal(",test@example.com\n", @site['SiteMember'].load)

    @session.run
    assert_match(%r|<title>|, @res.body.to_xhtml(0))

#    @req.parse_host_path('qwik.jp', '/test/0.html')
    @req.parse_host_path('qwik.jp', '/test/FrontPage.html')
    @req.query['c'] = 'edit'
    @session.run
    assert_match(%r|<title>.+</title|, @res.body.to_xhtml(0))

    @req.query['c'] = 'save'
    @req.query['p'] = '0'
    @req.query['md5hex'] = '0b316516113316e356cbd8b4a4e1a137'
    @req.query['contents'] = "* FrontPage\n[[test]]\n"
    @req.query['save'] = "保存"
    @session.run

#    @req.parse_host_path('qwik.jp', '/test/0.html')
    @req.parse_host_path('qwik.jp', '/test/FrontPage.html')
    @req.query['c'] = 'view'
    @session.run
  end

  def test_getpass
    @req.parse_host_path('qwik.jp', '/test/GetPass.html')
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @res.body
    assert_equal("パスワード入手", xml.elements['//h2'].text)

    @req.parse_host_path('qwik.jp', '/test/GetPass.html')
    @req.query['mail'] = 'test@example.com'
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @res.body
#    puts xml.to_xhtml(0).u8tosjis
    assert_equal("Send Password", xml.elements['//h2'].text)
    assert_equal('test@example.com', $smtp_sendmail[3])
  end

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