@ -1426,4 +1426,111 @@ describe('Post\'s', () => {
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
describe ( 'Topic Backlinks' , ( ) => {
let tid1 ;
before ( async ( ) => {
tid1 = await topics . post ( {
uid : 1 ,
cid ,
title : 'Topic backlink testing - topic 1' ,
content : 'Some text here for the OP' ,
} ) ;
tid1 = tid1 . topicData . tid ;
} ) ;
describe ( '.syncBacklinks()' , ( ) => {
it ( 'should error on invalid data' , async ( ) => {
try {
await topics . syncBacklinks ( ) ;
} catch ( e ) {
assert ( e ) ;
assert . strictEqual ( e . message , '[[error:invalid-data]]' ) ;
}
} ) ;
it ( 'should do nothing if the post does not contain a link to a topic' , async ( ) => {
const backlinks = await topics . syncBacklinks ( {
content : 'This is a post\'s content' ,
} ) ;
assert . strictEqual ( backlinks , 0 ) ;
} ) ;
it ( 'should create a backlink if it detects a topic link in a post' , async ( ) => {
const count = await topics . syncBacklinks ( {
pid : 2 ,
content : ` This is a link to [topic 1]( ${ nconf . get ( 'url' ) } /topic/1/abcdef) ` ,
} ) ;
const events = await topics . events . get ( 1 , 1 ) ;
const backlinks = await db . getSortedSetMembers ( 'pid:2:backlinks' ) ;
assert . strictEqual ( count , 1 ) ;
assert ( events ) ;
assert . strictEqual ( events . length , 1 ) ;
assert ( backlinks ) ;
assert ( backlinks . includes ( '1' ) ) ;
} ) ;
it ( 'should remove the backlink (but keep the event) if the post no longer contains a link to a topic' , async ( ) => {
const count = await topics . syncBacklinks ( {
pid : 2 ,
content : 'This is a link to [nothing](http://example.org)' ,
} ) ;
const events = await topics . events . get ( 1 , 1 ) ;
const backlinks = await db . getSortedSetMembers ( 'pid:2:backlinks' ) ;
assert . strictEqual ( count , 0 ) ;
assert ( events ) ;
assert . strictEqual ( events . length , 1 ) ;
assert ( backlinks ) ;
assert . strictEqual ( backlinks . length , 0 ) ;
} ) ;
} ) ;
describe ( 'integration tests' , ( ) => {
it ( 'should create a topic event in the referenced topic' , async ( ) => {
const topic = await topics . post ( {
uid : 1 ,
cid ,
title : 'Topic backlink testing - topic 2' ,
content : ` Some text here for the OP – ${ nconf . get ( 'url' ) } /topic/ ${ tid1 } ` ,
} ) ;
const events = await topics . events . get ( tid1 , 1 ) ;
assert ( events ) ;
assert . strictEqual ( events . length , 1 ) ;
assert . strictEqual ( events [ 0 ] . type , 'backlink' ) ;
assert . strictEqual ( parseInt ( events [ 0 ] . uid , 10 ) , 1 ) ;
assert . strictEqual ( events [ 0 ] . href , ` /post/ ${ topic . postData . pid } ` ) ;
} ) ;
it ( 'should not create a topic event if referenced topic is the same as current topic' , async ( ) => {
await topics . reply ( {
uid : 1 ,
tid : tid1 ,
content : ` Referencing itself – ${ nconf . get ( 'url' ) } /topic/ ${ tid1 } ` ,
} ) ;
const events = await topics . events . get ( tid1 , 1 ) ;
assert ( events ) ;
assert . strictEqual ( events . length , 1 ) ; // should still equal 1
} ) ;
it ( 'should not show backlink events if the feature is disabled' , async ( ) => {
meta . config . topicBacklinks = 0 ;
await topics . post ( {
uid : 1 ,
cid ,
title : 'Topic backlink testing - topic 3' ,
content : ` Some text here for the OP – ${ nconf . get ( 'url' ) } /topic/ ${ tid1 } ` ,
} ) ;
const events = await topics . events . get ( tid1 , 1 ) ;
assert ( events ) ;
assert . strictEqual ( events . length , 0 ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;