PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/topic_test.rb

https://github.com/nas/kete
Ruby | 641 lines | 465 code | 145 blank | 31 comment | 0 complexity | 56c61df24434d4e7e9708e6534dd082d MD5 | raw file
Possible License(s): GPL-3.0
  1. require File.dirname(__FILE__) + '/../test_helper'
  2. class TopicTest < Test::Unit::TestCase
  3. # fixtures preloaded
  4. def setup
  5. @base_class = "Topic"
  6. # Extend the base class so test files from attachment_fu get put in the
  7. # tmp directory, and not in the development/production directories.
  8. eval(@base_class).send(:include, ItemPrivacyTestHelper::Model)
  9. # hash of params to create new instance of model, e.g. {:name => 'Test Model', :description => 'Dummy'}
  10. @new_model = {
  11. :title => 'test item',
  12. :topic_type => TopicType.find(:first),
  13. :basket => Basket.find(:first)
  14. # :extended_content => { "first_names" => "Joe", "last_name" => "Bloggs" }
  15. }
  16. # name of fields that must be present, e.g. %(name description)
  17. @req_attr_names = %w(title)
  18. # name of fields that cannot be a duplicate, e.g. %(name description)
  19. @duplicate_attr_names = %w( )
  20. end
  21. # load in sets of tests and helper methods
  22. include KeteTestUnitHelper
  23. include HasContributorsTestUnitHelper
  24. include ExtendedContentTestUnitHelper
  25. include FlaggingTestUnitHelper
  26. include RelatedItemsTestUnitHelper
  27. include ItemPrivacyTestHelper::TestHelper
  28. include ItemPrivacyTestHelper::Tests::VersioningAndModeration
  29. include ItemPrivacyTestHelper::Tests::TaggingWithPrivacyContext
  30. include ItemPrivacyTestHelper::Tests::MovingItemsBetweenBasketsWithDifferentPrivacies
  31. def test_does_not_respond_to_file_private
  32. topic = Topic.create
  33. assert !topic.respond_to?(:file_private)
  34. assert !topic.respond_to?(:file_private=)
  35. end
  36. # Topic specific extended content tests
  37. def test_extended_content_setter
  38. model = Topic.new(@new_model.merge(:topic_type => TopicType.find_by_name("Person")))
  39. model.extended_content_values = { "first_names" => "Joe", "last_name" => "Bloggs" }
  40. model.save!
  41. assert_valid model
  42. assert_equal '<first_names xml_element_name="dc:description">Joe</first_names><last_name>Bloggs</last_name><place_of_birth xml_element_name="dc:subject"></place_of_birth>', model.extended_content
  43. end
  44. def test_xml_attributes
  45. model = Topic.new(@new_model.merge(:topic_type => TopicType.find_by_name("Person")))
  46. model.update_attribute(:extended_content, '<first_names xml_element_name="dc:description">Joe</first_names><last_name>Bloggs</last_name><place_of_birth xml_element_name="dc:subject"></place_of_birth>')
  47. assert_valid model
  48. assert_equal({ "1" => { "first_names" => "Joe" }, "2" => { "last_name" => "Bloggs" }, "3" => { "place_of_birth" => { "xml_element_name" => "dc:subject" } } }, model.xml_attributes)
  49. end
  50. def test_xml_attributes_without_data
  51. model = Topic.new(@new_model.merge(:topic_type => TopicType.find_by_name("Person")))
  52. model.update_attribute(:extended_content, '')
  53. assert model.valid?
  54. assert_equal({}, model.xml_attributes)
  55. end
  56. def test_xml_attributes_without_position_with_multiple_field_values
  57. for_topic_with(TopicType.find_by_name("Person"), { :label => "Address", :multiple => true}) do |t|
  58. t.extended_content_values = {
  59. "first_names" => "Joe",
  60. "last_name" => "Bloggs",
  61. "address" => { "1" => "The Parade", "2" => "Island Bay" }
  62. }
  63. assert_valid t
  64. assert_equal({
  65. "first_names"=> { "xml_element_name" => "dc:description", "value" => "Joe" },
  66. "address_multiple"=> {
  67. "1" => { "address" => { "xml_element_name" => "dc:description", "value" => "The Parade" } },
  68. "2" => { "address" => { "xml_element_name" => "dc:description", "value" => "Island Bay" } }
  69. },
  70. "place_of_birth" => { "xml_element_name" => "dc:subject" },
  71. "last_name" => "Bloggs" }, t.xml_attributes_without_position)
  72. end
  73. end
  74. def test_extended_content_pairs_with_multiple_field_values
  75. field = ExtendedField.create!(
  76. :label => "Address",
  77. :xml_element_name => "dc:description",
  78. :multiple => true,
  79. :ftype => "text"
  80. )
  81. topic_type = TopicType.find_by_name("Person")
  82. topic_type.form_fields << field
  83. topic_type.save!
  84. model = Topic.new(@new_model.merge(:topic_type => topic_type))
  85. model.extended_content_values = { "first_names" => "Joe", "last_name" => "Bloggs", "address" => { "1" => "Wollaston St.", "2" => "Nelson" } }
  86. assert_nothing_raised do
  87. model.save!
  88. end
  89. assert_equal [["first_names", "Joe"], ["last_name", "Bloggs"], ["address_multiple", [["Wollaston St."], ["Nelson"]]], ["place_of_birth", nil]].sort, \
  90. model.extended_content_pairs.sort
  91. end
  92. def test_extended_field_required_fields_are_validated
  93. # Test with valid fields
  94. model = Topic.new(@new_model.merge(:topic_type => TopicType.find_by_name("Person")))
  95. model.extended_content_values = { "first_names" => "Joe", "last_name" => "Bloggs", "city" => "Wellington" }
  96. assert_valid model
  97. assert_nothing_raised do
  98. model.save!
  99. end
  100. # Test with invalid fields
  101. model = Topic.new(@new_model.merge(:topic_type => TopicType.find_by_name("Person")))
  102. model.extended_content_values = { "first_names" => "", "last_name" => "Bloggs Fam." }
  103. assert_equal [["first_names", nil], ["last_name", "Bloggs Fam."], ["place_of_birth", nil]].sort, model.extended_content_pairs.sort
  104. assert !model.valid?
  105. assert_equal 1, model.errors.size
  106. assert_raises ActiveRecord::RecordInvalid do
  107. model.save!
  108. end
  109. end
  110. def test_extended_field_required_fields_are_validated_with_multiples
  111. topic_type = add_field_to(TopicType.find_by_name("Person"), { :label => "Address", :multiple => true }, { :required => true })
  112. model = Topic.new(@new_model.merge(:topic_type => topic_type))
  113. model.extended_content_values = { "first_names" => "Joe", "last_name" => "Bloggs", "address" => { "1" => "Wollaston St.", "2" => "" } }
  114. assert_valid model
  115. model.extended_content_values = { "first_names" => "Joe", "last_name" => "Bloggs", "address" => { "1" => "", "2" => "" } }
  116. assert !model.valid?
  117. assert_equal 1, model.errors.size
  118. # Drop our newly created field and mapping
  119. drop_last_field!
  120. end
  121. def test_helpers_work
  122. for_topic_with(TopicType.find_by_name("Person"), { :label => "Address", :ftype => "textarea" }) do |t|
  123. t.extended_content_values = { "first_names" => "Joe", "last_name" => "Bloggs" }
  124. assert_valid t
  125. assert_kind_of Topic, t
  126. end
  127. end
  128. def test_extended_field_text_fields_are_validated
  129. model = Topic.new(@new_model.merge(:topic_type => TopicType.find_by_name("Person")))
  130. model.extended_content_values = { "first_names" => "Joe", "last_name" => "Bloggs" }
  131. assert_valid model
  132. assert_equal 0, model.errors.size
  133. end
  134. def test_extended_field_textarea_fields_are_validated
  135. for_topic_with(TopicType.find_by_name("Person"), { :label => "Address", :ftype => "textarea" }) do |t|
  136. t.extended_content_values = { "first_names" => "Joe", "last_name" => "Bloggs", "address" => "New\n Line" }
  137. assert_valid t
  138. end
  139. end
  140. # TODO: We do not have a plan for how radio fields are to be used in Kete.
  141. #def test_extended_field_radio_fields_are_validated
  142. # print "Skipped"
  143. #end
  144. def test_extended_field_date_fields_are_validated
  145. for_topic_with(TopicType.find_by_name("Person"), { :label => "Birthdate", :ftype => "date" }) do |t|
  146. compulsory_content = { "first_names" => "Joe", "last_name" => "Bloggs" }
  147. t.extended_content_values = compulsory_content.merge("birthdate" => "1960-01-01")
  148. assert_valid t
  149. t.extended_content_values = compulsory_content.merge("birthdate" => "In 1960")
  150. assert !t.valid?
  151. assert_equal 1, t.errors.size
  152. assert_equal "Birthdate must be in the standard date format (YYYY-MM-DD)", t.errors.full_messages.join(", ")
  153. t.extended_content_values = compulsory_content.merge("birthdate" => "1960-1-1")
  154. assert !t.valid?
  155. assert_equal 1, t.errors.size
  156. assert_equal "Birthdate must be in the standard date format (YYYY-MM-DD)", t.errors.full_messages.join(", ")
  157. end
  158. end
  159. def test_extended_field_checkbox_fields_are_validated
  160. for_topic_with(TopicType.find_by_name("Person"), { :label => "Deceased", :ftype => "checkbox" }) do |t|
  161. compulsory_content = { "first_names" => "Joe", "last_name" => "Bloggs" }
  162. ["Yes", "No", "yes", "no", ""].each do |value|
  163. t.extended_content_values = compulsory_content.merge("deceased" => value)
  164. assert_valid t
  165. assert_equal 0, t.errors.size
  166. end
  167. [1, 0].each do |value|
  168. t.extended_content_values = compulsory_content.merge("deceased" => value)
  169. assert !t.valid?
  170. assert_equal "Deceased must be a valid checkbox value (Yes or No)", t.errors.full_messages.join(", ")
  171. end
  172. end
  173. end
  174. def test_extended_field_choice_fields_are_validated
  175. for_topic_with(TopicType.find_by_name("Person"), { :label => "Marital status", :ftype => "choice" }) do |t|
  176. compulsory_content = { "first_names" => "Joe", "last_name" => "Bloggs" }
  177. # Set up choices
  178. choice_content = [
  179. ["Married", "Married"],
  180. ["Defacto relationship", "Defacto Relationship"],
  181. ["Dating", "Dating"],
  182. ["Single", "Single"]
  183. ]
  184. choice_content.each do |l, v|
  185. c = Choice.create!(:label => l, :value => v)
  186. ExtendedField.last.choices << c
  187. end
  188. assert_equal 4, t.all_field_mappings.last.extended_field.choices.size
  189. # Run the tests
  190. ["", "Married", "Defacto Relationship", "Dating", "Single"].each do |value|
  191. t.extended_content_values = compulsory_content.merge("marital_status" => value)
  192. assert_valid t
  193. assert_equal 0, t.errors.size
  194. end
  195. ["married", "something else", "123", "Defacto", "Defacto relationship"].each do |v|
  196. t.extended_content_values = compulsory_content.merge("marital_status" => v)
  197. assert !t.valid?
  198. assert_equal 1, t.errors.size
  199. assert_equal "Marital status must be a valid choice", t.errors.full_messages.join(", ")
  200. end
  201. ExtendedField.last.choices.each { |c| c.destroy }
  202. assert_equal 0, ExtendedField.last.choices.size
  203. end
  204. end
  205. def test_adding_a_new_extended_field_renders_all_versions_invalid
  206. # Create a topic
  207. topic_type = TopicType.create!(:name => "Test", :description => "A test", :parent_id => 1)
  208. topic = Topic.create!(@new_model.merge(:topic_type => topic_type))
  209. # Update it and check that it's still valid
  210. topic.update_attributes! :description => "Changed description"
  211. assert_valid topic
  212. # Add a new required field to the topic type
  213. add_field_to(topic_type, { :label => "Is a test" }, :required => true)
  214. # The current version is still valid as it never had a value for the extended content.
  215. assert_valid topic
  216. # But updating the topic requires a value to be passed now, because it is a new requirement.
  217. assert !topic.update_attributes(:description => "Updated description again", :extended_content_values => { "is_a_test" => "" })
  218. assert !topic.valid?
  219. assert topic.update_attributes( \
  220. :description => "Updated description again",
  221. :extended_content_values => { "is_a_test" => "Yes" }
  222. )
  223. assert_valid topic
  224. end
  225. def test_empty_values_are_validated_correctly_on_new_records
  226. topic_type = TopicType.create!(:name => "Test", :description => "A test", :parent_id => 1)
  227. add_field_to(topic_type, { :label => "Is a test" }, :required => true)
  228. topic = Topic.new(@new_model.merge(:topic_type => topic_type))
  229. topic.extended_content_values = { "is_a_test" => "Yes" }
  230. assert_valid topic
  231. topic = Topic.new(@new_model.merge(:topic_type => topic_type))
  232. topic.extended_content_values = { "is_a_test" => "" }
  233. assert !topic.valid?
  234. end
  235. def test_empty_values_are_validated_correctly_on_existing_records
  236. topic_type = TopicType.create!(:name => "Test", :description => "A test", :parent_id => 1)
  237. topic = Topic.new(@new_model.merge(:topic_type => topic_type))
  238. assert topic.valid?
  239. add_field_to(topic_type, { :label => "Is a test" }, :required => true)
  240. assert topic.valid?
  241. topic.extended_content_values = { "is_a_test" => "Yes" }
  242. assert topic.valid?
  243. end
  244. def test_empty_values_are_validated_correctly_on_existing_records_with_multiples
  245. topic_type = TopicType.create!(:name => "Test", :description => "A test", :parent_id => 1)
  246. topic = Topic.new(@new_model.merge(:topic_type => topic_type))
  247. assert topic.valid?
  248. add_field_to(topic_type, { :multiple => true, :label => "Is a test" }, :required => true)
  249. assert topic.valid?
  250. topic.extended_content_values = { "is_a_test" => { "1" => "Yes" } }
  251. assert topic.valid?
  252. topic.extended_content_values = { "is_a_test" => { "1" => "" } }
  253. assert !topic.valid?
  254. topic.extended_content_values = nil
  255. assert topic.valid?
  256. end
  257. def test_empty_values_are_validated_correctly_on_existing_records_with_multiples_and_nil_values_disallowed
  258. topic_type = TopicType.create!(:name => "Test", :description => "A test", :parent_id => 1)
  259. topic = Topic.new(@new_model.merge(:topic_type => topic_type))
  260. topic.send(:allow_nil_values_for_extended_content=, false)
  261. assert topic.valid?
  262. assert_equal false, topic.send(:allow_nil_values_for_extended_content)
  263. add_field_to(topic_type, { :multiple => true, :label => "Is a test" }, :required => true)
  264. assert !topic.valid?
  265. topic.extended_content_values = { "is_a_test" => { "1" => "Yes" } }
  266. assert topic.valid?
  267. topic.extended_content_values = { "is_a_test" => { "1" => "" } }
  268. assert !topic.valid?
  269. topic.extended_content_values = nil
  270. assert !topic.valid?
  271. end
  272. def test_structured_extended_content_getter
  273. for_topic_with(TopicType.find_by_name("Person"), { :label => "Address", :multiple => true}) do |t|
  274. t.extended_content_values = {
  275. "first_names" => "Joe",
  276. "last_name" => "Bloggs",
  277. "address" => { "1" => "The Parade", "2" => "Island Bay" }
  278. }
  279. assert_valid t
  280. expected_hash = {
  281. "first_names" => [["Joe"]],
  282. "last_name" => [["Bloggs"]],
  283. "place_of_birth" => [[nil]],
  284. "address" => [["The Parade"], ["Island Bay"]]
  285. }
  286. assert_equal expected_hash, t.structured_extended_content
  287. end
  288. end
  289. def test_structured_extended_content_getter_with_choices
  290. for_topic_with(TopicType.find_by_name("Person"), { :label => "Marital status", :ftype => "choice", :multiple => false }) do |t|
  291. compulsory_content = { "first_names" => "Joe", "last_name" => "Bloggs" }
  292. # Set up choices
  293. choice_content = [
  294. ["Married", "Married"],
  295. ["Defacto relationship", "Defacto Relationship"],
  296. ["Dating", "Dating"],
  297. ["Single", "Single"]
  298. ]
  299. choice_content.each do |l, v|
  300. c = Choice.create!(:label => l, :value => v)
  301. ExtendedField.last.choices << c
  302. end
  303. t.extended_content_values = compulsory_content.merge("marital_status" => { "1" => "Married", "2" => "Dating" })
  304. assert_equal({ "first_names" => [["Joe"]], "last_name" => [["Bloggs"]], "marital_status" => [["Married", "Dating"]], "place_of_birth" => [[nil]] }, t.structured_extended_content)
  305. end
  306. end
  307. def test_structured_extended_content_getter_with_multiple_choices
  308. for_topic_with(TopicType.find_by_name("Person"), { :label => "Marital status", :ftype => "choice", :multiple => true }) do |t|
  309. compulsory_content = { "first_names" => "Joe", "last_name" => "Bloggs" }
  310. # Set up choices
  311. choice_content = [
  312. ["Married", "Married"],
  313. ["Defacto relationship", "Defacto Relationship"],
  314. ["Dating", "Dating"],
  315. ["Single", "Single"]
  316. ]
  317. choice_content.each do |l, v|
  318. c = Choice.create!(:label => l, :value => v)
  319. ExtendedField.last.choices << c
  320. end
  321. t.extended_content_values = compulsory_content.merge("marital_status" => { "1" => { "1" => "Married", "2" => "Dating" }, "2" => { "1" => "Single" } })
  322. assert_equal({ "first_names" => [["Joe"]], "last_name" => [["Bloggs"]], "marital_status" => [["Married", "Dating"], ["Single"]], "place_of_birth" => [[nil]] }, t.structured_extended_content)
  323. end
  324. end
  325. def test_structured_extended_content_getter_with_no_values
  326. for_topic_with(TopicType.find_by_name("Person"), { :label => "Address", :multiple => true}) do |t|
  327. t.extended_content = nil
  328. assert_equal({}, t.structured_extended_content)
  329. end
  330. end
  331. def test_structured_extended_content_setter
  332. for_topic_with(TopicType.find_by_name("Person"), { :label => "Address", :multiple => true}) do |t|
  333. t.structured_extended_content = {
  334. "first_names" => [["Joe"]],
  335. "last_name" => [["Bloggs"]],
  336. "place_of_birth" => [[nil]],
  337. "address" => [["The Parade"], ["Island Bay"]]
  338. }
  339. assert_valid t
  340. expected_value = '<first_names xml_element_name="dc:description">Joe</first_names><last_name>Bloggs</last_name><place_of_birth xml_element_name="dc:subject"></place_of_birth><address_multiple><1><address xml_element_name="dc:description">The Parade</address></1><2><address xml_element_name="dc:description">Island Bay</address></2></address_multiple>'
  341. assert_equal expected_value, t.extended_content
  342. end
  343. end
  344. def test_structured_extended_content_setter_with_choices
  345. for_topic_with(TopicType.find_by_name("Person"), { :label => "Marital status", :ftype => "choice", :multiple => false }) do |t|
  346. compulsory_content = { "first_names" => "Joe", "last_name" => "Bloggs" }
  347. # Set up choices
  348. choice_content = [
  349. ["Married", "Married"],
  350. ["Defacto relationship", "Defacto Relationship"],
  351. ["Dating", "Dating"],
  352. ["Single", "Single"]
  353. ]
  354. choice_content.each do |l, v|
  355. c = Choice.create!(:label => l, :value => v)
  356. ExtendedField.last.choices << c
  357. end
  358. t.structured_extended_content = {
  359. "first_names" => [["Joe"]],
  360. "last_name" => [["Bloggs"]],
  361. "marital_status" => [["Married", "Dating"]],
  362. "place_of_birth" => [[nil]]
  363. }
  364. expected_hash = {
  365. "first_names" => [["Joe"]],
  366. "marital_status" => [["Married", "Dating"]],
  367. "place_of_birth" => [[nil]],
  368. "last_name" => [["Bloggs"]]
  369. }
  370. assert_equal(expected_hash, t.structured_extended_content)
  371. expected_value = '<first_names xml_element_name="dc:description">Joe</first_names><last_name>Bloggs</last_name><place_of_birth xml_element_name="dc:subject"></place_of_birth><marital_status xml_element_name="dc:description"><1>Married</1><2>Dating</2></marital_status>'
  372. assert_equal expected_value, t.extended_content
  373. end
  374. end
  375. def test_structured_extended_content_setter_with_multiple_choices
  376. for_topic_with(TopicType.find_by_name("Person"), { :label => "Marital status", :ftype => "choice", :multiple => true }) do |t|
  377. compulsory_content = { "first_names" => "Joe", "last_name" => "Bloggs" }
  378. # Set up choices
  379. choice_content = [
  380. ["Married", "Married"],
  381. ["Defacto relationship", "Defacto Relationship"],
  382. ["Dating", "Dating"],
  383. ["Single", "Single"]
  384. ]
  385. choice_content.each do |l, v|
  386. c = Choice.create!(:label => l, :value => v)
  387. ExtendedField.last.choices << c
  388. end
  389. t.structured_extended_content = {
  390. "first_names" => [["Joe"]],
  391. "last_name" => [["Bloggs"]],
  392. "marital_status" => [["Married", "Dating"], ["Single"]],
  393. "place_of_birth" => [[nil]]
  394. }
  395. expected_hash = {
  396. "first_names" => [["Joe"]],
  397. "marital_status" => [["Married", "Dating"], ["Single"]],
  398. "place_of_birth" => [[nil]],
  399. "last_name" => [["Bloggs"]]
  400. }
  401. assert_equal(expected_hash, t.structured_extended_content)
  402. expected_value = '<first_names xml_element_name="dc:description">Joe</first_names><last_name>Bloggs</last_name><place_of_birth xml_element_name="dc:subject"></place_of_birth><marital_status_multiple><1><marital_status xml_element_name="dc:description"><1>Married</1><2>Dating</2></marital_status></1><2><marital_status xml_element_name="dc:description"><1>Single</1></marital_status></2></marital_status_multiple>'
  403. assert_equal expected_value, t.extended_content
  404. end
  405. end
  406. def test_extended_content_accessors_with_multiple_choices
  407. for_topic_with(TopicType.find_by_name("Person"), { :label => "Marital status", :ftype => "choice", :multiple => true }) do |t|
  408. compulsory_content = { "first_names" => "Joe", "last_name" => "Bloggs" }
  409. # Set up choices
  410. choice_content = [
  411. ["Married", "Married"],
  412. ["Defacto relationship", "Defacto Relationship"],
  413. ["Dating", "Dating"],
  414. ["Single", "Single"]
  415. ]
  416. choice_content.each do |l, v|
  417. c = Choice.create!(:label => l, :value => v)
  418. ExtendedField.last.choices << c
  419. end
  420. t.structured_extended_content = {
  421. "first_names" => [["Joe"]],
  422. "last_name" => [["Bloggs"]],
  423. "marital_status" => [["Married", "Dating"], ["Single"]],
  424. "place_of_birth" => [[nil]]
  425. }
  426. assert_equal "Joe", t.first_names
  427. assert_equal ["Married -> Dating", "Single"], t.marital_status
  428. assert_equal "", t.place_of_birth
  429. t.marital_status = "Married"
  430. assert_equal "Married", t.marital_status
  431. assert t.extended_content.include?("<1><marital_status xml_element_name=\"dc:description\">Married</marital_status></1>")
  432. t.send("marital_status+=", "Single")
  433. assert_equal ["Married", "Single"], t.marital_status
  434. expected = "<1><marital_status xml_element_name=\"dc:description\">Married</marital_status></1>"
  435. expected += "<2><marital_status xml_element_name=\"dc:description\">Single</marital_status></2>"
  436. assert t.extended_content.include?(expected), "#{expected} should be in extended content, but isn't. #{t.extended_content}"
  437. t.send("first_names+=", " John")
  438. assert_equal "Joe John", t.first_names
  439. end
  440. end
  441. def test_extended_content_accessors_with_choices
  442. for_topic_with(TopicType.find_by_name("Person"), { :label => "Marital status", :ftype => "choice", :multiple => false }) do |t|
  443. compulsory_content = { "first_names" => "Joe", "last_name" => "Bloggs" }
  444. # Set up choices
  445. choice_content = [
  446. ["Married", "Married"],
  447. ["Defacto relationship", "Defacto Relationship"],
  448. ["Dating", "Dating"],
  449. ["Single", "Single"]
  450. ]
  451. choice_content.each do |l, v|
  452. c = Choice.create!(:label => l, :value => v)
  453. ExtendedField.last.choices << c
  454. end
  455. t.structured_extended_content = {
  456. "first_names" => [["Joe"]],
  457. "last_name" => [["Bloggs"]],
  458. "marital_status" => [["Married", "Dating"]],
  459. "place_of_birth" => [[nil]]
  460. }
  461. assert_equal "Joe", t.first_names
  462. assert_equal "Married -> Dating", t.marital_status
  463. assert_equal "", t.place_of_birth
  464. t.marital_status = "Single"
  465. assert_equal "Single", t.marital_status
  466. assert t.extended_content.include?("<marital_status xml_element_name=\"dc:description\"><1>Single</1></marital_status>")
  467. end
  468. end
  469. protected
  470. # Some helpers for extended field tests
  471. # Returns instance of TopicType.
  472. def add_field_to(topic_type, field_attribute_hash = {}, mapping_options = {})
  473. default_field_attributes = {
  474. :label => "Test",
  475. :xml_element_name => "dc:description",
  476. :multiple => false,
  477. :ftype => "text"
  478. }
  479. mapping_attributes = {
  480. :extended_field => ExtendedField.create!(default_field_attributes.merge(field_attribute_hash)),
  481. :required => false
  482. }
  483. topic_type.topic_type_to_field_mappings.create(mapping_attributes.merge(mapping_options))
  484. topic_type
  485. end
  486. def for_topic_with(topic_type, field_attribute_hash = {}, mapping_options = {})
  487. tt = add_field_to(topic_type, field_attribute_hash, mapping_options)
  488. model = Topic.new(@new_model.merge(:topic_type => tt))
  489. yield(model)
  490. drop_last_field!
  491. end
  492. def drop_last_field!
  493. ExtendedField.last.destroy
  494. TopicTypeToFieldMapping.last.destroy
  495. end
  496. end