|
| 1 | +//! Shared test utilities for utf8dok-ooxml |
| 2 | +//! |
| 3 | +//! This module provides common fixtures and helpers used across tests. |
| 4 | +
|
| 5 | +use std::io::{Cursor, Write}; |
| 6 | +use zip::write::SimpleFileOptions; |
| 7 | +use zip::CompressionMethod; |
| 8 | +use zip::ZipWriter; |
| 9 | + |
| 10 | +use crate::archive::OoxmlArchive; |
| 11 | + |
| 12 | +/// Create a minimal valid DOCX template for testing |
| 13 | +/// |
| 14 | +/// This creates a valid DOCX ZIP structure with: |
| 15 | +/// - [Content_Types].xml |
| 16 | +/// - _rels/.rels |
| 17 | +/// - word/_rels/document.xml.rels |
| 18 | +/// - word/document.xml (placeholder content) |
| 19 | +/// |
| 20 | +/// # Example |
| 21 | +/// ```ignore |
| 22 | +/// use utf8dok_ooxml::test_utils::create_minimal_template; |
| 23 | +/// let template = create_minimal_template(); |
| 24 | +/// ``` |
| 25 | +pub fn create_minimal_template() -> Vec<u8> { |
| 26 | + let mut buffer = Cursor::new(Vec::new()); |
| 27 | + let mut zip = ZipWriter::new(&mut buffer); |
| 28 | + let options = SimpleFileOptions::default().compression_method(CompressionMethod::Stored); |
| 29 | + |
| 30 | + // [Content_Types].xml |
| 31 | + zip.start_file("[Content_Types].xml", options).unwrap(); |
| 32 | + zip.write_all( |
| 33 | + br#"<?xml version="1.0" encoding="UTF-8"?> |
| 34 | +<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> |
| 35 | + <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> |
| 36 | + <Default Extension="xml" ContentType="application/xml"/> |
| 37 | + <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> |
| 38 | +</Types>"#, |
| 39 | + ) |
| 40 | + .unwrap(); |
| 41 | + |
| 42 | + // _rels/.rels |
| 43 | + zip.start_file("_rels/.rels", options).unwrap(); |
| 44 | + zip.write_all( |
| 45 | + br#"<?xml version="1.0" encoding="UTF-8"?> |
| 46 | +<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> |
| 47 | + <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/> |
| 48 | +</Relationships>"#, |
| 49 | + ) |
| 50 | + .unwrap(); |
| 51 | + |
| 52 | + // word/_rels/document.xml.rels |
| 53 | + zip.start_file("word/_rels/document.xml.rels", options) |
| 54 | + .unwrap(); |
| 55 | + zip.write_all( |
| 56 | + br#"<?xml version="1.0" encoding="UTF-8"?> |
| 57 | +<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> |
| 58 | +</Relationships>"#, |
| 59 | + ) |
| 60 | + .unwrap(); |
| 61 | + |
| 62 | + // word/document.xml (placeholder, will be replaced) |
| 63 | + zip.start_file("word/document.xml", options).unwrap(); |
| 64 | + zip.write_all( |
| 65 | + br#"<?xml version="1.0" encoding="UTF-8"?> |
| 66 | +<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> |
| 67 | + <w:body> |
| 68 | + <w:p><w:r><w:t>Template</w:t></w:r></w:p> |
| 69 | + </w:body> |
| 70 | +</w:document>"#, |
| 71 | + ) |
| 72 | + .unwrap(); |
| 73 | + |
| 74 | + zip.finish().unwrap(); |
| 75 | + buffer.into_inner() |
| 76 | +} |
| 77 | + |
| 78 | +/// Create a minimal DOCX template with styles.xml |
| 79 | +/// |
| 80 | +/// Includes basic heading styles for testing style-aware features. |
| 81 | +pub fn create_template_with_styles() -> Vec<u8> { |
| 82 | + let mut buffer = Cursor::new(Vec::new()); |
| 83 | + let mut zip = ZipWriter::new(&mut buffer); |
| 84 | + let options = SimpleFileOptions::default().compression_method(CompressionMethod::Stored); |
| 85 | + |
| 86 | + // [Content_Types].xml |
| 87 | + zip.start_file("[Content_Types].xml", options).unwrap(); |
| 88 | + zip.write_all( |
| 89 | + br#"<?xml version="1.0" encoding="UTF-8"?> |
| 90 | +<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> |
| 91 | + <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> |
| 92 | + <Default Extension="xml" ContentType="application/xml"/> |
| 93 | + <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> |
| 94 | + <Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/> |
| 95 | +</Types>"#, |
| 96 | + ) |
| 97 | + .unwrap(); |
| 98 | + |
| 99 | + // _rels/.rels |
| 100 | + zip.start_file("_rels/.rels", options).unwrap(); |
| 101 | + zip.write_all( |
| 102 | + br#"<?xml version="1.0" encoding="UTF-8"?> |
| 103 | +<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> |
| 104 | + <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/> |
| 105 | +</Relationships>"#, |
| 106 | + ) |
| 107 | + .unwrap(); |
| 108 | + |
| 109 | + // word/_rels/document.xml.rels |
| 110 | + zip.start_file("word/_rels/document.xml.rels", options) |
| 111 | + .unwrap(); |
| 112 | + zip.write_all( |
| 113 | + br#"<?xml version="1.0" encoding="UTF-8"?> |
| 114 | +<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> |
| 115 | + <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/> |
| 116 | +</Relationships>"#, |
| 117 | + ) |
| 118 | + .unwrap(); |
| 119 | + |
| 120 | + // word/styles.xml |
| 121 | + zip.start_file("word/styles.xml", options).unwrap(); |
| 122 | + zip.write_all( |
| 123 | + br#"<?xml version="1.0" encoding="UTF-8"?> |
| 124 | +<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> |
| 125 | + <w:style w:type="paragraph" w:styleId="Heading1"> |
| 126 | + <w:name w:val="heading 1"/> |
| 127 | + <w:pPr><w:outlineLvl w:val="0"/></w:pPr> |
| 128 | + </w:style> |
| 129 | + <w:style w:type="paragraph" w:styleId="Heading2"> |
| 130 | + <w:name w:val="heading 2"/> |
| 131 | + <w:pPr><w:outlineLvl w:val="1"/></w:pPr> |
| 132 | + </w:style> |
| 133 | + <w:style w:type="paragraph" w:styleId="Normal"> |
| 134 | + <w:name w:val="Normal"/> |
| 135 | + </w:style> |
| 136 | +</w:styles>"#, |
| 137 | + ) |
| 138 | + .unwrap(); |
| 139 | + |
| 140 | + // word/document.xml |
| 141 | + zip.start_file("word/document.xml", options).unwrap(); |
| 142 | + zip.write_all( |
| 143 | + br#"<?xml version="1.0" encoding="UTF-8"?> |
| 144 | +<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> |
| 145 | + <w:body> |
| 146 | + <w:p><w:r><w:t>Template with styles</w:t></w:r></w:p> |
| 147 | + </w:body> |
| 148 | +</w:document>"#, |
| 149 | + ) |
| 150 | + .unwrap(); |
| 151 | + |
| 152 | + zip.finish().unwrap(); |
| 153 | + buffer.into_inner() |
| 154 | +} |
| 155 | + |
| 156 | +/// Extract document.xml content from a DOCX byte array |
| 157 | +pub fn extract_document_xml(docx: &[u8]) -> String { |
| 158 | + let cursor = Cursor::new(docx); |
| 159 | + let archive = OoxmlArchive::from_reader(cursor).unwrap(); |
| 160 | + archive.get_string("word/document.xml").unwrap().unwrap() |
| 161 | +} |
| 162 | + |
| 163 | +/// Extract any file content from a DOCX byte array |
| 164 | +pub fn extract_file(docx: &[u8], path: &str) -> Option<String> { |
| 165 | + let cursor = Cursor::new(docx); |
| 166 | + let archive = OoxmlArchive::from_reader(cursor).unwrap(); |
| 167 | + archive.get_string(path).unwrap() |
| 168 | +} |
| 169 | + |
| 170 | +#[cfg(test)] |
| 171 | +mod tests { |
| 172 | + use super::*; |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn test_create_minimal_template() { |
| 176 | + let template = create_minimal_template(); |
| 177 | + assert!(!template.is_empty()); |
| 178 | + |
| 179 | + // Verify it's a valid ZIP |
| 180 | + let cursor = Cursor::new(&template); |
| 181 | + let archive = OoxmlArchive::from_reader(cursor).unwrap(); |
| 182 | + |
| 183 | + assert!(archive.contains("[Content_Types].xml")); |
| 184 | + assert!(archive.contains("word/document.xml")); |
| 185 | + assert!(archive.contains("_rels/.rels")); |
| 186 | + } |
| 187 | + |
| 188 | + #[test] |
| 189 | + fn test_create_template_with_styles() { |
| 190 | + let template = create_template_with_styles(); |
| 191 | + assert!(!template.is_empty()); |
| 192 | + |
| 193 | + let cursor = Cursor::new(&template); |
| 194 | + let archive = OoxmlArchive::from_reader(cursor).unwrap(); |
| 195 | + |
| 196 | + assert!(archive.contains("word/styles.xml")); |
| 197 | + |
| 198 | + let styles = archive.get_string("word/styles.xml").unwrap().unwrap(); |
| 199 | + assert!(styles.contains("Heading1")); |
| 200 | + assert!(styles.contains("Heading2")); |
| 201 | + } |
| 202 | + |
| 203 | + #[test] |
| 204 | + fn test_extract_document_xml() { |
| 205 | + let template = create_minimal_template(); |
| 206 | + let doc_xml = extract_document_xml(&template); |
| 207 | + |
| 208 | + assert!(doc_xml.contains("w:document")); |
| 209 | + assert!(doc_xml.contains("Template")); |
| 210 | + } |
| 211 | + |
| 212 | + #[test] |
| 213 | + fn test_extract_file() { |
| 214 | + let template = create_minimal_template(); |
| 215 | + |
| 216 | + let content_types = extract_file(&template, "[Content_Types].xml"); |
| 217 | + assert!(content_types.is_some()); |
| 218 | + assert!(content_types.unwrap().contains("Types")); |
| 219 | + |
| 220 | + let missing = extract_file(&template, "nonexistent.xml"); |
| 221 | + assert!(missing.is_none()); |
| 222 | + } |
| 223 | +} |
0 commit comments