# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/act-newpage"

if $0 == __FILE__
  require "qwik/test-common"
  $debug = true
end

if $debug
  class TC_ActNewPageWithJapanese < Test::Unit::TestCase
    include TestSession

    def test_embeded_newpage_with_japanese_name
      t_add_user
      page = @site["FrontPage"]
      page.store("[[最初のページ]]")
      assert_equal("FrontPage", page.get_title)

      # see a page
      session("/test/")
      assert_in(["最初のページ"], "//span[@class='new']")
      #pw("//div[@class='section']")
      assert_xpath([:a, {:href=>".new?t=%8D%C5%8F%89%82%CC%83y%81%5B%83W",
		       :class=>"new"}, [:img, {:src=>"/.theme/i/new.png",
			 :alt=>"create", :class=>"new"}]],
		   "//div[@class='section']/a")

      # Please input corresponding page key
      session("/test/.new?t=最初のページ")
      assert_text("New page", "h1")
      assert_text("Confirm", "h2")
      assert_attr({"action"=>".new", "method"=>"post"}, "form")
      assert_attr({"name"=>"t", "value"=>"最初のページ"}, "input")
      assert_attr({"type"=>"submit", "value"=>"New page"}, "input[2]")

      # POST
      session("POST /test/.new?t=最初のページ")
      assert_text("New page", "h1")
      assert_xml("Edit new page", "//div[@class='section']//a")
      assert_rattr({"href"=>"1.edit"}, "//div[@class='section']//a")

      # check the new page
      assert_equal("* 最初のページ\n", @site["1"].load)
      page = @site.get_by_title("最初のページ")
      assert_equal("1", page.key)

      # check title
      page = @site["1"]
      assert_equal("最初のページ", page.get_title)
      assert_equal("1", @site.get_by_title("最初のページ").key.to_s)
      assert_equal(false, @site.exist?("最初のページ"))
      assert_equal(true, @site.exist?("1"))

      # check the source page
      session("/test/")
      assert_xml("最初のページ", "//div[@class='section']//a")
      assert_rattr({"href"=>"1.html"}, "//div[@class='section']//a")
      assert_equal("[[最初のページ]]", @site["FrontPage"].load)

      # Try to create with the same key.
      session("/test/.new?t=最初のページ")
      #pw("//div[@class='section']")

      # Try to create with the same key.
      session("POST /test/.new?t=最初のページ")
      #pw("//div[@class='section']")
      assert_in(["Already exist"], "//h2")

      # Let's create a page again.  At the first, embed the title.
      page = @site["FrontPage"]
      page.store("[[二頁]]")

      # See the page
      session("/test/")
      assert_xml("二頁", "//div[@class='section']//span")
      assert_rattr({"href"=>".new?t=%93%F1%95%C5", "class"=>"new"},
		   "//div[@class='section']//a")

      # please input a corresponding page key
      session("/test/.new?t=二頁")
      assert_attr({"name"=>"t", "value"=>"二頁"}, "input")

      # try again
      session("POST /test/.new?t=二頁")
      assert_xml("Edit new page", "//div[@class='section']//a")
      assert_rattr({"href"=>"2.edit"}, "//div[@class='section']//a")

      page = @site["2"]
      assert_equal("* 二頁\n", page.load)

      # check the source page again
      session("/test/")
      assert_xml("二頁", "//div[@class='section']//a")
      assert_rattr({"href"=>"2.html"}, "//div[@class='section']//a")
      assert_equal("[[二頁]]", @site["FrontPage"].load)
    end
  end
end
