#!/usr/bin/env ruby
# $Id: tc_ext_rss.rb,v 1.7 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/server'
require 'qwik/site'
require 'qwik/ext_rss'

class TC_RSS < 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')
    pages = @site.pages
    page = pages.create("1")
    page.store("* あ")
  end

  def test_rss
    # test_rss091
    rssgen = Qwik::RSS091Generator.new(@config, @site)
    xml = rssgen.generate
    assert_equal('<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">', xml.doctype.to_s)
    assert_equal("0.91", xml.elements['/rss'].attribute('version').to_s)
    assert_equal("qwik.jp/test", xml.elements['/rss/channel/title'].text.to_s)
    assert_equal("http://qwik.jp/test/", xml.elements['/rss/channel/link'].text.to_s)

    # test_rss10
    rssgen = Qwik::RSS10Generator.new(@config, @site)
    xml = rssgen.generate
    #puts xml.to_s(0)
    assert_equal("http://purl.org/rss/1.0/", xml.elements['/rdf:RDF'].attribute('xmlns').to_s)
  end

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

class TC_RssGet < 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)
    @req.cookies['user'] = 'test@example.com'
    @req.cookies['pass'] = 'E4268F8D'
  end

  def test_get_rss
    @req.parse_host_path('qwik.jp', '/test/test.rss')
    @site = Qwik::Site.new(@config, 'test')
    @site.member.add('test@example.com')
    @site.pages.create_new.store('* あ')

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

    assert_equal("<a href='test.rss'>RSS</a>", "{{rss}}".w.html_p(@site))

    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    str = @res.body
#    puts xml.to_xhtml(0)
    assert_equal("application/xml", @res['Content-Type'])
    assert_match(/qwik.jp\/test/, str)
  end

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