#!/usr/bin/env ruby
# $Id: tc_ext_sitelog.rb,v 1.4 2004/05/12 18:23:34 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.

require "test_common"

class TC_SiteLog < Test::Unit::TestCase
  include TestSite

  def test_sitelog_unit
#    assert_equal(nil, @site["_SiteLog"])
    sitelog = @site.sitelog
    sitelog.add(0, "test@example.com", "save", "1")
    assert_equal(",0,test@example.com,save,1\n", @site["_SiteLog"].load)
    sitelog.add(0, nil, "save", "1")
    assert_equal(",0,test@example.com,save,1\n,0,,save,1\n", @site["_SiteLog"].load)
  end

  def test_sitelog
    @site.member.add("test@example.com")
#    assert_equal(nil, @site["_SiteLog"])

    @req.parse_host_path("qwik.jp", "/test/_new.html")
    @req.query["f"] = nil # not specify
    @req.query["t"] = "テストページ"
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
    assert_equal("新規ページ作成", xml.elements['//h1'].text)
    e = xml.elements['//a']
    assert_equal("新規ページ編集画面", e.text)
    assert_equal("1.html?c=edit", e.attribute("href").to_s)
    assert_match(/,test@example.com,create,1\n/, @site["_SiteLog"].load)

    @req.parse_host_path("qwik.jp", "/test/1.html")
    @req.query["c"] = "edit"
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
    assert_equal("* テストページ\n", xml.elements['//textarea'].text)

    @req.parse_host_path("qwik.jp", "/test/1.html")
    @req.query["c"] = "save"
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
    assert_equal("ページ生成エラー", xml.elements['//h1'].text)

    @req.query["contents"] = "テストページの内容です"
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
    assert_equal("ページを保存しました", xml.elements['//h1'].text)
    assert_match(/,test@example.com,save,1\n/, @site["_SiteLog"].load)

    @req.query["contents"] = "" # to delete the page
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
    assert_equal("ページを削除しました", xml.elements['//h1'].text)
    assert_match(/,test@example.com,create,1\n/, @site["_SiteLog"].load)
    assert_match(/,test@example.com,save,1\n/, @site["_SiteLog"].load)
    assert_match(/,test@example.com,delete,1\n/, @site["_SiteLog"].load)

    #puts @site["_SiteLog"].load
    #qp @site.sitelog.db.hash
    #qp @site.sitelog.list
#    assert_equal("", @site.sitelog.list)

    page = @site.create_new
    page.store("{{sitelog}}")
    xml = page.load.w.res(@site)
    assert_match(/<dd>save: <a href='1.html'>1<\/a>/, xml.to_s)
#"<body><p><dl><dt>2004-05-11 23:21:12: test@example.com</dt><dd>save: <a href='1.html'>1</a></dd><dt>2004-05-11 23:21:13: test@example.com</dt><dd>delete: <a href='1.html'>1</a></dd></dl></p>\n</body>"


  end
end
