2019-08-06 23:17:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class PostTest < ActiveSupport::TestCase
|
|
|
|
setup do
|
|
|
|
# Trabajamos con el sitio de sutty porque tiene artículos
|
|
|
|
#
|
|
|
|
# TODO: Cambiar a skel cuando publiquemos los códigos y privacidad
|
|
|
|
@site = create :site, name: 'sutty.nl'
|
|
|
|
@site.read
|
|
|
|
@post = @site.posts.sample
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
# @site.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se puede acceder a los valores' do
|
|
|
|
assert @site.posts.size.positive?
|
|
|
|
|
|
|
|
assert @post.categories.values.size.positive?
|
|
|
|
assert @post.tags.values.size.positive?
|
|
|
|
assert @post.title.size.positive?
|
|
|
|
assert @post.content.size.positive?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'no se puede setear cualquier atributo' do
|
|
|
|
assert_raise NoMethodError do
|
|
|
|
@post.verdura = 'verdura'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se pueden eliminar' do
|
|
|
|
# TODO: cuando esté con git, solo aplicar git reset
|
|
|
|
tmp = File.join(Rails.root, 'tmp', 'eliminar.md')
|
|
|
|
FileUtils.cp @post.path, tmp
|
|
|
|
|
|
|
|
assert @post.destroy
|
|
|
|
assert_not File.exist?(@post.path)
|
|
|
|
assert_not @site.posts.include?(@post)
|
|
|
|
|
|
|
|
FileUtils.mv tmp, @post.path
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se puede ver el contenido completo' do
|
|
|
|
tmp = Tempfile.new
|
|
|
|
|
|
|
|
begin
|
|
|
|
tmp.write(@post.full_content)
|
|
|
|
tmp.close
|
|
|
|
|
|
|
|
collection = Jekyll::Collection.new(@site.jekyll, I18n.locale.to_s)
|
|
|
|
document = Jekyll::Document.new(tmp.path, site: @site.jekyll,
|
|
|
|
collection: collection)
|
|
|
|
document.read
|
|
|
|
document.data['categories'].try(:delete_if) do |x|
|
|
|
|
x == 'tmp'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Queremos saber si todos los atributos del post terminaron en el
|
|
|
|
# archivo
|
|
|
|
@post.attributes.each do |attr|
|
|
|
|
template = @post.send(attr)
|
|
|
|
# ignorar atributos que no son datos
|
|
|
|
next unless template.is_a? MetadataTemplate
|
|
|
|
|
|
|
|
if template.empty?
|
|
|
|
assert_not document.data[attr.to_s].present?
|
|
|
|
else
|
|
|
|
assert_equal template.value, document.data[attr.to_s]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
tmp.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se pueden validar' do
|
|
|
|
assert @post.valid?
|
|
|
|
|
|
|
|
# XXX: si usamos nil va a traer el valor original del documento, no
|
|
|
|
# queremos tener esa información sincronizada...
|
|
|
|
@post.title.value = ''
|
|
|
|
|
|
|
|
assert_not @post.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'se pueden guardar los cambios' do
|
2019-08-07 15:10:14 +00:00
|
|
|
title = SecureRandom.hex
|
|
|
|
@post.title.value = title
|
|
|
|
@post.categories.value << title
|
|
|
|
|
|
|
|
assert @post.save
|
|
|
|
|
|
|
|
Dir.chdir(@site.path) do
|
|
|
|
collection = Jekyll::Collection.new(@site.jekyll, I18n.locale.to_s)
|
|
|
|
document = Jekyll::Document.new(@post.path,
|
|
|
|
site: @site.jekyll,
|
|
|
|
collection: collection)
|
|
|
|
document.read
|
|
|
|
|
|
|
|
assert document.data['categories'].include?(title)
|
|
|
|
assert_equal title, document.data['title']
|
|
|
|
assert_equal 'post', document.data['layout']
|
|
|
|
end
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
|
|
|
end
|